summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Wrock <matt@mattwrock.com>2016-08-30 14:47:43 -0700
committerGitHub <noreply@github.com>2016-08-30 14:47:43 -0700
commitf7ed14d0692aa5aaa86325df3710a0c7708d4357 (patch)
treeb76fdf7ab0cccc932ff9ed50ac5798624be412ae
parent83d0c5ca3535583bc6560b594e236567616d6b52 (diff)
parent5793388bb1388543f1cdf52dfa4bf56c8c4ef66d (diff)
downloadmixlib-config-f7ed14d0692aa5aaa86325df3710a0c7708d4357.tar.gz
Merge pull request #40 from chef/restore
Restore by swapping in saved state instead of 'reset and merge'
-rw-r--r--lib/mixlib/config.rb14
-rw-r--r--spec/mixlib/config_spec.rb29
2 files changed, 35 insertions, 8 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb
index 40c5926..517aae7 100644
--- a/lib/mixlib/config.rb
+++ b/lib/mixlib/config.rb
@@ -177,16 +177,20 @@ 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.
- #
+ #
# === Returns
# self
def restore(hash)
- reset
- merge!(hash)
+ self.configuration = hash.reject { |key, value| config_contexts.has_key?(key) }
+ config_contexts.each do |key, config_context|
+ if hash.has_key?(key)
+ config_context.restore(hash[key])
+ else
+ config_context.reset
+ end
+ end
end
# Merge an incoming hash with our config options
diff --git a/spec/mixlib/config_spec.rb b/spec/mixlib/config_spec.rb
index cc1f872..9948b7f 100644
--- a/spec/mixlib/config_spec.rb
+++ b/spec/mixlib/config_spec.rb
@@ -788,6 +788,7 @@ describe Mixlib::Config do
config_context(:blah) do
config_context(:yarr) do
default :x, 5
+ default :y, 6
end
end
configurable :x
@@ -796,11 +797,14 @@ describe Mixlib::Config do
it "configurable defaults in that context work" do
expect(@klass.blah.yarr.x).to eql(5)
+ expect(@klass.blah.yarr.y).to eql(6)
end
it "after setting values in the context, the values remain set" do
@klass.blah.yarr.x = 10
+ @klass.blah.yarr.y = 11
expect(@klass.blah.yarr.x).to eql(10)
+ expect(@klass.blah.yarr.y).to eql(11)
end
it "setting values with the same name in the parent context do not affect the child context" do
@@ -811,9 +815,12 @@ describe Mixlib::Config do
it "after reset of the parent class, children are reset" do
@klass.blah.yarr.x = 10
+ @klass.blah.yarr.y = 11
expect(@klass.blah.yarr.x).to eql(10)
+ expect(@klass.blah.yarr.y).to eql(11)
@klass.reset
expect(@klass.blah.yarr.x).to eql(5)
+ expect(@klass.blah.yarr.y).to eql(6)
end
it "save should not save anything for it by default" do
@@ -821,17 +828,33 @@ describe Mixlib::Config do
end
it "save with include_defaults should save all defaults" do
- expect(@klass.save(true)).to eql({ :blah => { :yarr => { :x => 5 } } })
+ expect(@klass.save(true)).to eql({ :blah => { :yarr => { :x => 5, :y => 6 } } })
end
it "saves any new values that are set in the context" do
@klass.blah.yarr.x = 10
- expect((saved = @klass.save)).to eql({ :blah => { :yarr => { :x => 10 } } })
+ @klass.blah.yarr.y = 11
+ expect((saved = @klass.save)).to eql({ :blah => { :yarr => { :x => 10, :y => 11 } } })
@klass.reset
expect(@klass.blah.yarr.x).to eql(5)
+ expect(@klass.blah.yarr.y).to eql(6)
@klass.restore(saved)
expect(@klass.blah.yarr.x).to eql(10)
- expect(@klass.save).to eql({ :blah => { :yarr => { :x => 10 } } })
+ expect(@klass.blah.yarr.y).to eql(11)
+ expect(@klass.save).to eql({ :blah => { :yarr => { :x => 10, :y => 11 } } })
+ end
+
+ it "restores defaults not included in saved data" do
+ @klass.restore( :blah => { :yarr => { :x => 10 } } )
+ expect(@klass.blah.yarr.x).to eql(10)
+ expect(@klass.blah.yarr.y).to eql(6)
+ end
+
+ it "resmoves added properties not included in saved state" do
+ @klass.blah.yarr.z = 12
+ @klass.restore( :blah => { :yarr => { :x => 10 } } )
+ expect(@klass.blah.yarr.x).to eql(10)
+ expect(@klass.blah.yarr.z).to eql(nil)
end
end