diff options
Diffstat (limited to 'lib/mixlib/config.rb')
-rw-r--r-- | lib/mixlib/config.rb | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb index 835521b..14699b0 100644 --- a/lib/mixlib/config.rb +++ b/lib/mixlib/config.rb @@ -1,6 +1,7 @@ # # Author:: Adam Jacob (<adam@opscode.com>) # Author:: Nuo Yan (<nuo@opscode.com>) +# Author:: Christopher Brown (<cb@opscode.com>) # Copyright:: Copyright (c) 2008 Opscode, Inc. # License:: Apache License, Version 2.0 # @@ -16,6 +17,13 @@ # See the License for the specific language governing permissions and # 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 @@ -32,11 +40,7 @@ module Mixlib # === Parameters # <string>:: A filename to read from def from_file(filename) - begin - self.instance_eval(IO.read(filename), filename, 1) - rescue Exception => e - raise IOError, "Cannot open or read #{filename}!" + e - end + self.instance_eval(IO.read(filename), filename, 1) end # Pass Mixlib::Config.configure() a block, and it will yield self.configuration. @@ -128,8 +132,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| + self.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. @@ -145,11 +163,10 @@ 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? - self.configuration[method_symbol] = (num_args == 1 ? args[0] : args) + internal_set method_symbol, (num_args == 1 ? args[0] : args) end # Returning |