Skip to content

Commit

Permalink
WIP: Changes to work with deep_merge so we can merge in nil values
Browse files Browse the repository at this point in the history
  • Loading branch information
jrafanie committed Apr 25, 2017
1 parent 243cc6c commit 311c024
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions Appraisals
Expand Up @@ -18,4 +18,5 @@ end

appraise 'rails-5' do
gem 'rails', '5.0.1'
gem "deep_merge", :path => "/Users/joerafaniello/Code/deep_merge"
end
2 changes: 1 addition & 1 deletion gemfiles/rails_5.gemfile
Expand Up @@ -3,5 +3,5 @@
source "https://rubygems.org"

gem "rails", "5.0.1"

gem "deep_merge", :path => "/Users/joerafaniello/Code/deep_merge"
gemspec :path => "../"
1 change: 1 addition & 0 deletions lib/config/options.rb
Expand Up @@ -124,6 +124,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 # fails

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 311c024

Please sign in to comment.