diff options
-rw-r--r-- | lib/chef/mixin/params_validate.rb | 12 | ||||
-rw-r--r-- | spec/unit/property_spec.rb | 47 |
2 files changed, 29 insertions, 30 deletions
diff --git a/lib/chef/mixin/params_validate.rb b/lib/chef/mixin/params_validate.rb index 926387b888..c920bdc992 100644 --- a/lib/chef/mixin/params_validate.rb +++ b/lib/chef/mixin/params_validate.rb @@ -94,7 +94,9 @@ class Chef if self.instance_variable_defined?(iv_symbol) value = self.instance_variable_get(iv_symbol) if value.is_a?(DelayedEvaluator) - value = validate({ symbol => value.call }, { symbol => validation })[symbol] + # Pass optional first parameter of self + value = value.call(self) + value = validate({ symbol => value }, { symbol => validation })[symbol] end # Get the default value @@ -107,7 +109,11 @@ class Chef # this case, the block yields an optional parameter of +self+, # which is the equivalent of "new_resource" if value.is_a?(DelayedEvaluator) - value = value.call(self) + if value.arity >= 1 + value = value.call(self) + else + value = instance_eval(&value) + end end # Defaults are presently "stickily" set on the instance @@ -281,7 +287,7 @@ class Chef if opts.has_key?(key.to_s) opts[key.to_s] = instance_exec(opts[key], &coercer) elsif opts.has_key?(key.to_sym) - opts[key.to_sym] = instance_exec(opts[key], &coercer) + opts[key.to_sym] = instance_exec(opts[key], &coercer) end end end diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb index fe6863f3bd..919be5b61b 100644 --- a/spec/unit/property_spec.rb +++ b/spec/unit/property_spec.rb @@ -401,16 +401,13 @@ describe "Chef::Resource.property" do end with_property ':x, default: lazy { blah }' do - # it "x is run in context of the instance" do - # expect(resource.x).to eq "blah1" - # end - # it "x is run in the context of each instance it is run in" do - # expect(resource.x).to eq "blah1" - # expect(resource_class.new('another').x).to eq "another2" - # expect(resource.x).to eq "blah3" - # end - it "x is run outside the instance" do - expect(resource.x).to eq "class" + it "x is run in context of the instance" do + expect(resource.x).to eq "blah1" + end + it "x is run in the context of each instance it is run in" do + expect(resource.x).to eq "blah1" + expect(resource_class.new('another').x).to eq "another2" + # expect(resource.x).to eq "blah3" end end @@ -584,10 +581,6 @@ describe "Chef::Resource.property" do def blah "example" end - it "retrieving lazy { blah } gets the caller's blah" do - resource.x lazy { blah } - expect(resource.x).to eq "example" - end # it "retrieving lazy { blah } gets the instance variable" do # resource.x lazy { blah } # expect(resource.x).to eq "blah1" @@ -601,19 +594,19 @@ describe "Chef::Resource.property" do # expect(resource.x).to eq "blah2" # expect(resource2.x).to eq "another3" # end - # it 'retrieving lazy { |x| "#{blah}#{x.blah}" } gets the example and instance variables' do - # resource.x lazy { |x| "#{blah}#{x.blah}" } - # expect(resource.x).to eq "exampleblah1" - # end - # it 'retrieving lazy { |x| "#{blah}#{x.blah}" } from two different instances gets two different instance variables' do - # resource2 = resource_class.new("another") - # l = lazy { |x| "#{blah}#{x.blah}" } - # resource2.x l - # resource.x l - # expect(resource2.x).to eq "exampleanother1" - # expect(resource.x).to eq "exampleblah2" - # expect(resource2.x).to eq "exampleanother3" - # end + it 'retrieving lazy { |x| "#{blah}#{x.blah}" } gets the example and instance variables' do + resource.x lazy { |x| "#{blah}#{x.blah}" } + expect(resource.x).to eq "exampleblah1" + end + it 'retrieving lazy { |x| "#{blah}#{x.blah}" } from two different instances gets two different instance variables' do + resource2 = resource_class.new("another") + l = lazy { |x| "#{blah}#{x.blah}" } + resource2.x l + resource.x l + expect(resource2.x).to eq "exampleanother1" + expect(resource.x).to eq "exampleblah2" + expect(resource2.x).to eq "exampleanother3" + end end end |