diff options
author | Kierran McPherson <kierranm@gmail.com> | 2016-08-11 09:18:37 +1200 |
---|---|---|
committer | Kierran McPherson <kierranm@gmail.com> | 2016-08-13 10:07:11 +1200 |
commit | b6260dab4d5c447106b49ce8b35687e23260b311 (patch) | |
tree | 68dd659eee5bbec838086a922d1949b7075b854e | |
parent | 05db78c270ef91d17cd79e347502a021805a43ed (diff) | |
download | mixlib-config-b6260dab4d5c447106b49ce8b35687e23260b311.tar.gz |
Allow configuring contexts via block
-rw-r--r-- | lib/mixlib/config.rb | 13 | ||||
-rw-r--r-- | spec/mixlib/config_spec.rb | 9 |
2 files changed, 20 insertions, 2 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb index 07e9809..0da23b7 100644 --- a/lib/mixlib/config.rb +++ b/lib/mixlib/config.rb @@ -457,9 +457,18 @@ module Mixlib internal_set(symbol, value) end # Getter - meta.send :define_method, symbol do |*args| - internal_get_or_set(symbol, *args) + meta.send :define_method, symbol do |*args, &block| + # If a block was given, eval it in the context + if block + context_eval(symbol, &block) + else + internal_get_or_set(symbol, *args) + end end end + + def context_eval(context, &block) + internal_get(context).instance_eval(&block) + end end end diff --git a/spec/mixlib/config_spec.rb b/spec/mixlib/config_spec.rb index 9f9e320..1ea8d02 100644 --- a/spec/mixlib/config_spec.rb +++ b/spec/mixlib/config_spec.rb @@ -754,6 +754,15 @@ describe Mixlib::Config do @klass.blah.z.should == 10 end + it "setting the context values in a block overrides the default values" do + @klass.blah do + x 10 + 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 |