diff options
author | Christopher Brown <cb@opscode.com> | 2009-08-24 15:14:13 -0700 |
---|---|---|
committer | Christopher Brown <cb@opscode.com> | 2009-08-24 15:14:13 -0700 |
commit | 8970135a7225a7ee8508d73978c4e44d3c7c953d (patch) | |
tree | ca7d6087135bdca0453e05e3a9c010cd25fbedc1 /lib/mixlib/config.rb | |
parent | 535d7c04b71f59826e719e5a9b37a9425fe9ba76 (diff) | |
download | mixlib-config-internal_set_fix.tar.gz |
added config_attr_writerinternal_set_fix
Diffstat (limited to 'lib/mixlib/config.rb')
-rw-r--r-- | lib/mixlib/config.rb | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb index edc1a08..2fd0c94 100644 --- a/lib/mixlib/config.rb +++ b/lib/mixlib/config.rb @@ -16,6 +16,12 @@ # limitations under the License. # +class Object # http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html + def meta_def name, &blk + (class << self; self; end).instance_eval { define_method name, &blk } + end +end + module Mixlib module Config @@ -121,8 +127,22 @@ module Mixlib end end - private :internal_set + protected :internal_set + # metaprogramming to ensure that the slot for method_symbol + # gets set to value after any other logic is run + # === Parameters + # method_symbol<Symbol>:: Name of the method (variable setter) + # blk<Block>:: logic block to run in setting slot method_symbol to value + # value<Object>:: Value to be set in config hash + # + def config_attr_writer(method_symbol, &blk) + method_name = "#{method_symbol.to_s}=" + meta_def method_name do |value| + @@configuration[method_symbol] = blk.call(value) + end + end + # Allows for simple lookups and setting of configuration options via method calls # on Mixlib::Config. If there any arguments to the method, they are used to set # the value of the configuration option. Otherwise, it's a simple get operation. @@ -138,7 +158,6 @@ module Mixlib # <ArgumentError>:: If the method_symbol does not match a configuration option. def method_missing(method_symbol, *args) num_args = args.length - # Setting if num_args > 0 method_symbol = $1.to_sym unless (method_symbol.to_s =~ /(.+)=$/).nil? |