lib/mixlib/config.rb in mixlib-config-2.2.12 vs lib/mixlib/config.rb in mixlib-config-2.2.13

- old
+ new

@@ -68,20 +68,20 @@ # # === Parameters # filename<String>:: A filename to read from def from_yaml(filename) require "yaml" - from_hash(YAML.load(IO.read(filename)), filename) + from_hash(YAML.load(IO.read(filename))) end # Parses valid JSON structure into Ruby # # === Parameters # filename<String>:: A filename to read from def from_json(filename) require "json" - from_hash(JSON.parse(IO.read(filename)), filename) + from_hash(JSON.parse(IO.read(filename))) end def from_toml(filename) require "tomlrb" from_hash(Tomlrb.parse(IO.read(filename), symbolize_keys: true)) @@ -89,24 +89,12 @@ # Transforms a Hash into method-style configuration syntax to be processed # # === Parameters # hash<Hash>:: A Hash containing configuration - def from_hash(hash, filename = "in_memory") - ruby_translation = [] - - to_dotted_hash(hash).each do |k, v| - if v.is_a? Array - ruby_translation << "#{k} #{v}" - elsif v.is_a? String - ruby_translation << "#{k} \"#{v}\"" - else - ruby_translation << "#{k} #{v}" - end - end - - instance_eval(ruby_translation.join("\n"), filename, 1) + def from_hash(hash) + apply_nested_hash(hash) end # Pass Mixlib::Config.configure() a block, and it will yield itself # # === Parameters @@ -563,9 +551,28 @@ # === Raises # <UnknownConfigOptionError>:: If the config option does not exist and strict mode is on. def method_missing(method_symbol, *args) method_symbol = $1.to_sym if method_symbol.to_s =~ /(.+)=$/ internal_get_or_set(method_symbol, *args) + end + + protected + + # Given a (nested) Hash, apply it to the config object and any contexts. + # + # This is preferable to converting it to the string representation with + # the #to_dotted_hash method above. + # + # === 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) + else + internal_set(k.to_sym, v) + end + end end private # Given a (nested) Hash, turn it into a single top-level hash using dots as