From 50d3f80d495938e3793983d87325a2ebbb625342 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Fri, 21 Apr 2017 16:48:08 -0400 Subject: [PATCH] Changes to work with deep_merge so we merge in nil values https://github.com/danielsdeleo/deep_merge/pull/33 was merged and released in deep_merge 1.2.0+. Fixes #13 --- lib/config/options.rb | 1 + spec/config_spec.rb | 33 ++++++++++++++++++++++++++-- spec/fixtures/deep_merge/config1.yml | 3 +++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/config/options.rb b/lib/config/options.rb index 69247df1..ea2b7bda 100644 --- a/lib/config/options.rb +++ b/lib/config/options.rb @@ -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) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 736bcd5f..6925db84 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -198,7 +198,8 @@ 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) { { 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 } @@ -216,6 +217,34 @@ it 'should rewrite a merged value' do expect { config.merge!(hash) }.to change { config.inner.something1 }.from('blah1').to('changed1') 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 @@ -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 diff --git a/spec/fixtures/deep_merge/config1.yml b/spec/fixtures/deep_merge/config1.yml index 0ffefd8f..1ba74eda 100644 --- a/spec/fixtures/deep_merge/config1.yml +++ b/spec/fixtures/deep_merge/config1.yml @@ -3,6 +3,9 @@ server: google.com inner: something1: "blah1" something2: "blah2" + somethingnil: + somethingfalse: false + somethingtrue: true inner2: inner2_inner: