lib/mixlib/config.rb in mixlib-config-3.0.1 vs lib/mixlib/config.rb in mixlib-config-3.0.5

- old
+ new

@@ -31,15 +31,15 @@ class << base; attr_accessor :configurables; end class << base; attr_accessor :config_contexts; end class << base; attr_accessor :config_context_lists; end class << base; attr_accessor :config_context_hashes; end class << base; attr_accessor :config_parent; end - base.configuration = Hash.new - base.configurables = Hash.new - base.config_contexts = Hash.new - base.config_context_lists = Hash.new - base.config_context_hashes = Hash.new + base.configuration = ({}) + base.configurables = ({}) + base.config_contexts = ({}) + base.config_context_lists = ({}) + base.config_context_hashes = ({}) base.initialize_mixlib_config end def initialize_mixlib_config @config_strict_mode = nil @@ -163,12 +163,12 @@ configuration.delete(symbol) end # Resets all config options to their defaults. def reset - self.configuration = Hash.new - config_contexts.values.each { |config_context| config_context.reset } + self.configuration = ({}) + config_contexts.values.each(&:reset) end # Makes a copy of any non-default values. # # This returns a shallow copy of the hash; while the hash itself is @@ -370,10 +370,11 @@ def configurable(symbol, &block) unless configurables[symbol] if config_contexts.key?(symbol) raise ReopenedConfigContextWithConfigurableError, "Cannot redefine config_context #{symbol} as a configurable value" end + configurables[symbol] = Configurable.new(symbol) define_attr_accessor_methods(symbol) end if block yield(configurables[symbol]) @@ -529,13 +530,14 @@ # # === Raises # <ArgumentError>:: if value is set to something other than true, false, or :warn # def config_strict_mode=(value) - if ![ true, false, :warn, nil ].include?(value) + unless [ true, false, :warn, nil ].include?(value) raise ArgumentError, "config_strict_mode must be true, false, nil or :warn" end + @config_strict_mode = value end # Allows for simple lookups and setting of config options via method calls # on Mixlib::Config. If there any arguments to the method, they are used to set @@ -565,10 +567,13 @@ # === Parameters # hash<Hash>:: The hash to apply to the config oject def apply_nested_hash(hash) hash.each do |k, v| if v.is_a? Hash - internal_get(k.to_sym).apply_nested_hash(v) + # If loading from hash, and we reference a context that doesn't exist + # and warning/strict is off, we need to create the config context that we expected to be here. + context = internal_get(k.to_sym) || config_context(k.to_sym) + context.apply_nested_hash(v) else internal_set(k.to_sym, v) end end end