Skip to content

Commit

Permalink
Changes to work with deep_merge so we merge in nil values
Browse files Browse the repository at this point in the history
  • Loading branch information
jrafanie committed Jan 31, 2018
1 parent 2ad2542 commit 18c743b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/config/options.rb
Expand Up @@ -127,6 +127,7 @@ def merge!(hash)
current = to_hash
DeepMerge.deep_merge!(hash.dup,
current,
merge_nil_values: true,
preserve_unmergeables: false,
overwrite_arrays: Config.overwrite_arrays)
marshal_load(__convert(current).marshal_dump)
Expand Down
33 changes: 31 additions & 2 deletions spec/config_spec.rb
Expand Up @@ -199,6 +199,7 @@
context "Merging nested hash at runtime" do
let(:config) { Config.load_files("#{fixture_path}/deep_merge/config1.yml") }
let(:hash) { { :inner => { :something1 => 'changed1', :something3 => 'changed3' } } }
let(:hash_with_nil) { { :inner => { :something1 => nil } } }

it 'should preserve first level keys' do
expect { config.merge!(hash) }.to_not change { config.keys }
Expand All @@ -216,7 +217,35 @@
it 'should rewrite a merged value' do
expect { config.merge!(hash) }.to change { config.inner.something1 }.from('blah1').to('changed1')
end
end

it 'should update a string to nil ' do
expect { config.merge!(hash_with_nil) }.to change { config.inner.something1 }.from('blah1').to(nil)
end

it 'should update something nil to true' do
expect { config.merge!({:inner => {:somethingnil => true}}) }.to change { config.inner.somethingnil }.from(nil).to(true)
end

it 'should update something nil to false' do
expect { config.merge!({:inner => {:somethingnil => false}}) }.to change { config.inner.somethingnil }.from(nil).to(false)
end

it 'should update something false to true' do
expect { config.merge!({:inner => {:somethingfalse => true}}) }.to change { config.inner.somethingfalse }.from(false).to(true)
end

it 'should update something false to nil' do
expect { config.merge!({:inner => {:somethingfalse => nil}}) }.to change { config.inner.somethingfalse }.from(false).to(nil)
end

it 'should update something true to false' do
expect { config.merge!({:inner => {:somethingtrue => false}}) }.to change { config.inner.somethingtrue }.from(true).to(false)
end

it 'should update something true to nil' do
expect { config.merge!({:inner => {:somethingtrue => nil}}) }.to change { config.inner.somethingtrue }.from(true).to(nil)
end
end

context "[] accessors" do
let(:config) do
Expand Down Expand Up @@ -355,7 +384,7 @@
end

it 'should merge hashes from multiple configs' do
expect(config.inner.marshal_dump.keys.size).to eq(3)
expect(config.inner.marshal_dump.keys.size).to eq(6)
expect(config.inner2.inner2_inner.marshal_dump.keys.size).to eq(3)
end

Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/deep_merge/config1.yml
Expand Up @@ -3,6 +3,9 @@ server: google.com
inner:
something1: "blah1"
something2: "blah2"
somethingnil:
somethingfalse: false
somethingtrue: true

inner2:
inner2_inner:
Expand Down

0 comments on commit 18c743b

Please sign in to comment.