diff options
author | Matt Wrock <matt@mattwrock.com> | 2016-08-29 15:58:03 -0700 |
---|---|---|
committer | Matt Wrock <matt@mattwrock.com> | 2016-08-29 15:58:03 -0700 |
commit | 2ec57d3b27daeeee152da7a73df57cb033d8e3aa (patch) | |
tree | 52ea0681959bebbc35b132ae579d41a44c95b065 | |
parent | 83d0c5ca3535583bc6560b594e236567616d6b52 (diff) | |
download | mixlib-config-restore.tar.gz |
restore using 'mirror and swap' instead or 'reset and merge'restore
-rw-r--r-- | lib/mixlib/config.rb | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb index 40c5926..4654c7e 100644 --- a/lib/mixlib/config.rb +++ b/lib/mixlib/config.rb @@ -177,16 +177,28 @@ module Mixlib # Restore non-default values from the given hash. # - # This method is the equivalent of +reset+ followed by +merge!(hash)+. - # # === Parameters # hash<Hash>: a hash in the same format as output by save. - # + # mirrored_context<Config>: a config with schema to mirror + # # === Returns # self - def restore(hash) - reset - merge!(hash) + def restore(hash, mirrored_context = self) + new_config = Hash.new + new_contexts = Hash.new + hash.each do |key, value| + if mirrored_context.config_contexts.has_key?(key) + context = Class.new + context.extend(::Mixlib::Config) + context.config_parent = self + context.restore(value, mirrored_context.config_contexts[key]) + new_contexts[key] = context + else + new_config[key] = value + end + end + self.configuration = new_config + self.config_contexts = new_contexts end # Merge an incoming hash with our config options |