diff options
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? |