diff options
author | Kierran McPherson <kierranm@gmail.com> | 2016-08-12 08:01:21 +1200 |
---|---|---|
committer | Kierran McPherson <kierranm@gmail.com> | 2016-08-13 10:07:42 +1200 |
commit | 63c2774edd8ad9568d033e2985fd705eb9c99eb7 (patch) | |
tree | b256e3d4bfeb881f35c5fef9f6418fbb865a7693 | |
parent | b6260dab4d5c447106b49ce8b35687e23260b311 (diff) | |
download | mixlib-config-63c2774edd8ad9568d033e2985fd705eb9c99eb7.tar.gz |
Also allow yielding the context
-rw-r--r-- | lib/mixlib/config.rb | 11 | ||||
-rw-r--r-- | spec/mixlib/config_spec.rb | 9 |
2 files changed, 19 insertions, 1 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb index 0da23b7..c039364 100644 --- a/lib/mixlib/config.rb +++ b/lib/mixlib/config.rb @@ -460,7 +460,12 @@ module Mixlib meta.send :define_method, symbol do |*args, &block| # If a block was given, eval it in the context if block - context_eval(symbol, &block) + # If the block expects no arguments, then instance_eval + if block.arity == 0 + context_eval(symbol, &block) + else # yield to the block + context_yield(symbol, &block) + end else internal_get_or_set(symbol, *args) end @@ -470,5 +475,9 @@ module Mixlib def context_eval(context, &block) internal_get(context).instance_eval(&block) end + + def context_yield(context) + yield internal_get(context) + end end end diff --git a/spec/mixlib/config_spec.rb b/spec/mixlib/config_spec.rb index 1ea8d02..f6b2b1c 100644 --- a/spec/mixlib/config_spec.rb +++ b/spec/mixlib/config_spec.rb @@ -763,6 +763,15 @@ describe Mixlib::Config do @klass.blah.y.should == 20 end + it "setting the context values in a yielded block overrides the default values" do + @klass.blah do |b| + b.x = 10 + b.y = 20 + end + @klass.blah.x.should == 10 + @klass.blah.y.should == 20 + end + it "after reset of the parent class, children are reset" do @klass.blah.x = 10 @klass.blah.x.should == 10 |