From 311c024e75c3813840dccfb84bccccdb5d5d10cb Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Fri, 21 Apr 2017 16:48:08 -0400 Subject: [PATCH] WIP: Changes to work with deep_merge so we can merge in nil values https://github.com/danielsdeleo/deep_merge/pull/33 Similar to https://github.com/railsconfig/config/issues/13 --- Appraisals | 1 + gemfiles/rails_5.gemfile | 2 +- lib/config/options.rb | 1 + spec/config_spec.rb | 33 ++++++++++++++++++++++++++-- spec/fixtures/deep_merge/config1.yml | 3 +++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Appraisals b/Appraisals index 60e65099..987c77cf 100644 --- a/Appraisals +++ b/Appraisals @@ -18,4 +18,5 @@ end appraise 'rails-5' do gem 'rails', '5.0.1' + gem "deep_merge", :path => "/Users/joerafaniello/Code/deep_merge" end diff --git a/gemfiles/rails_5.gemfile b/gemfiles/rails_5.gemfile index 04324335..82099c93 100644 --- a/gemfiles/rails_5.gemfile +++ b/gemfiles/rails_5.gemfile @@ -3,5 +3,5 @@ source "https://rubygems.org" gem "rails", "5.0.1" - +gem "deep_merge", :path => "/Users/joerafaniello/Code/deep_merge" gemspec :path => "../" diff --git a/lib/config/options.rb b/lib/config/options.rb index 92e8c2b0..9e65c9ac 100644 --- a/lib/config/options.rb +++ b/lib/config/options.rb @@ -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) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 736bcd5f..6faf5720 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -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 } @@ -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 @@ -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: