lib/mixlib/config.rb in mixlib-config-1.0.12 vs lib/mixlib/config.rb in mixlib-config-1.1.0.rc01

- old
+ new

@@ -1,7 +1,9 @@ # # 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 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,21 +16,18 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 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 + + def self.extended(base) + class << base; attr_accessor :configuration; end + base.configuration = Hash.new + end - @@configuration = Hash.new - # Loads a given ruby file, and runs instance_eval against it in the context of the current # object. # # Raises an IOError if the file cannot be found, or is not readable. # @@ -36,18 +35,18 @@ # <string>:: A filename to read from def from_file(filename) self.instance_eval(IO.read(filename), filename, 1) end - # Pass Mixlib::Config.configure() a block, and it will yield @@configuration. + # Pass Mixlib::Config.configure() a block, and it will yield self.configuration. # # === Parameters - # <block>:: A block that is sent @@configuration as its argument + # <block>:: A block that is sent self.configuration as its argument def configure(&block) - block.call(@@configuration) + block.call(self.configuration) end - + # Get the value of a configuration option # # === Parameters # config_option<Symbol>:: The configuration option to return # @@ -55,11 +54,11 @@ # value:: The value of the configuration option # # === Raises # <ArgumentError>:: If the configuration option does not exist def [](config_option) - @@configuration[config_option.to_sym] + self.configuration[config_option.to_sym] end # Set the value of a configuration option # # === Parameters @@ -79,38 +78,38 @@ # # === Returns # <True>:: If the configuration option exists # <False>:: If the configuration option does not exist def has_key?(key) - @@configuration.has_key?(key.to_sym) + self.configuration.has_key?(key.to_sym) end # Merge an incoming hash with our config options # # === Parameters # hash<Hash>:: The incoming hash # # === Returns # result of Hash#merge! def merge!(hash) - @@configuration.merge!(hash) + self.configuration.merge!(hash) end # Return the set of config hash keys # # === Returns # result of Hash#keys def keys - @@configuration.keys + self.configuration.keys end # Creates a shallow copy of the internal hash # # === Returns # result of Hash#dup def hash_dup - @@configuration.dup + self.configuration.dup end # Internal dispatch setter, calling either the real defined method or setting the # hash value directly # @@ -118,14 +117,14 @@ # method_symbol<Symbol>:: Name of the method (variable setter) # value<Object>:: Value to be set in config hash # def internal_set(method_symbol,value) method_name = method_symbol.id2name - if (self.public_methods - ["[]="]).include?("#{method_name}=") + if self.respond_to?("#{method_name}=".to_sym) self.send("#{method_name}=", value) else - @@configuration[method_symbol] = value + self.configuration[method_symbol] = value end end protected :internal_set @@ -135,13 +134,14 @@ # 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) + meta = class << self; self; end + method_name = "#{method_symbol.to_s}=".to_sym + meta.send :define_method, 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 @@ -163,10 +163,10 @@ method_symbol = $1.to_sym unless (method_symbol.to_s =~ /(.+)=$/).nil? internal_set method_symbol, (num_args == 1 ? args[0] : args) end # Returning - @@configuration[method_symbol] + self.configuration[method_symbol] end end end