summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Roberts <chrisroberts.code@gmail.com>2012-12-27 09:16:44 -0800
committerBryan McLellan <btm@opscode.com>2013-05-24 11:18:29 -0700
commitdb616869b7ace2cd853370685d025e6ac62d3cb1 (patch)
tree0a88c9c4276864894fef1ad22c411bf7d0b2eb26
parent4eae072fd47c883b988f0192eff41a0641fb9e40 (diff)
downloadchef-db616869b7ace2cd853370685d025e6ac62d3cb1.tar.gz
Allow delayed attribute evaluation. Add tests for delayed evaluation
-rw-r--r--chef/lib/chef/mixin/params_validate.rb31
-rw-r--r--chef/spec/unit/mixin/params_validate_spec.rb21
2 files changed, 44 insertions, 8 deletions
diff --git a/chef/lib/chef/mixin/params_validate.rb b/chef/lib/chef/mixin/params_validate.rb
index 649224f978..4266e69580 100644
--- a/chef/lib/chef/mixin/params_validate.rb
+++ b/chef/lib/chef/mixin/params_validate.rb
@@ -16,7 +16,8 @@
# limitations under the License.
class Chef
-
+ class DelayedEvaluator < Proc
+ end
module Mixin
module ParamsValidate
@@ -75,18 +76,32 @@ class Chef
end
opts
end
-
- def set_or_return(symbol, arg, validation)
+
+ def delay_eval(&block)
+ DelayedEvaluator.new(&block)
+ end
+
+ def set_or_return(symbol, arg, validation, &block)
iv_symbol = "@#{symbol.to_s}".to_sym
map = {
symbol => validation
}
-
- if arg == nil && self.instance_variable_defined?(iv_symbol) == true
- self.instance_variable_get(iv_symbol)
+ if arg == nil && self.instance_variable_defined?(iv_symbol) == true && !block_given?
+ ivar = self.instance_variable_get(iv_symbol)
+ if(ivar.is_a?(DelayedEvaluator))
+ validate({ symbol => ivar.call }, { symbol => validation })[symbol]
+ else
+ ivar
+ end
else
- opts = validate({ symbol => arg }, { symbol => validation })
- self.instance_variable_set(iv_symbol, opts[symbol])
+ if(arg.is_a?(DelayedEvaluator))
+ val = arg
+ elsif(block_given?)
+ val = DelayedEvaluator.new(&block)
+ else
+ val = validate({ symbol => arg }, { symbol => validation })[symbol]
+ end
+ self.instance_variable_set(iv_symbol, val)
end
end
diff --git a/chef/spec/unit/mixin/params_validate_spec.rb b/chef/spec/unit/mixin/params_validate_spec.rb
index dd0366c37c..25c0c17f1e 100644
--- a/chef/spec/unit/mixin/params_validate_spec.rb
+++ b/chef/spec/unit/mixin/params_validate_spec.rb
@@ -366,5 +366,26 @@ describe Chef::Mixin::ParamsValidate do
@vo.set_or_return(:name, value, { }).object_id.should == value.object_id
@vo.set_or_return(:foo, nil, { :name_attribute => true }).object_id.should == value.object_id
end
+
+ it "should allow DynamicEvaluator instance to be set for value regardless of restriction" do
+ value = Chef::DelayedEvaluator.new{ 'test' }
+ @vo.set_or_return(:test, value, {:kind_of => String})
+ @vo.set_or_return(:test, nil, {:kind_of => String}).should == 'test'
+ end
+
+ it "should raise an error when evaluated attribute is not valid" do
+ value = Chef::DelayedEvaluator.new{ 'test' }
+ @vo.set_or_return(:test, value, {:kind_of => Numeric})
+ lambda do
+ @vo.set_or_return(:test, nil, {:kind_of => Numeric})
+ end.should raise_error(Chef::Exceptions::ValidationFailed)
+ end
+
+ it "should not evaluate non DelayedEvaluator instances" do
+ value = lambda{ 'test' }
+ @vo.set_or_return(:test, value, {})
+ @vo.set_or_return(:test, nil, {}).object_id.should == value.object_id
+ @vo.set_or_return(:test, nil, {}).should be_a(Proc)
+ end
end