From 6c425b19314494642745b3a4466b98a4277e1f5e Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 10 Jan 2019 17:48:39 -0100 Subject: [PATCH 1/5] Add merge_hash_arrays as an option to pass to DeepMerge --- lib/config.rb | 3 ++- lib/config/options.rb | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/config.rb b/lib/config.rb index a6322641..44dc9b28 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -26,10 +26,11 @@ module Config @@fail_on_missing = false # deep_merge options - mattr_accessor :knockout_prefix, :merge_nil_values, :overwrite_arrays + mattr_accessor :knockout_prefix, :merge_nil_values, :overwrite_arrays, :merge_hash_arrays @@knockout_prefix = nil @@merge_nil_values = true @@overwrite_arrays = true + @@merge_hash_arrays = false def self.setup yield self if @@_ran_once == false diff --git a/lib/config/options.rb b/lib/config/options.rb index d5396fc3..054f1c0f 100644 --- a/lib/config/options.rb +++ b/lib/config/options.rb @@ -82,7 +82,8 @@ def reload! preserve_unmergeables: false, knockout_prefix: Config.knockout_prefix, overwrite_arrays: Config.overwrite_arrays, - merge_nil_values: Config.merge_nil_values + merge_nil_values: Config.merge_nil_values, + merge_hash_arrays: Config.merge_hash_arrays ) end end @@ -134,7 +135,8 @@ def merge!(hash) preserve_unmergeables: false, knockout_prefix: Config.knockout_prefix, overwrite_arrays: Config.overwrite_arrays, - merge_nil_values: Config.merge_nil_values + merge_nil_values: Config.merge_nil_values, + merge_hash_arrays: Config.merge_hash_arrays ) marshal_load(__convert(current).marshal_dump) self From 0aa0ebee1488720237c42c2196cbe5d933ceedb9 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 10 Jan 2019 23:10:26 -0100 Subject: [PATCH 2/5] Add some tests & update readme for supporting merge_hash_arrays --- CHANGELOG.md | 4 ++- README.md | 1 + spec/fixtures/deep_merge3/.config1.yml.swp | Bin 0 -> 12288 bytes spec/fixtures/deep_merge3/.config2.yml.swp | Bin 0 -> 12288 bytes spec/fixtures/deep_merge3/config1.yml | 1 + spec/fixtures/deep_merge3/config2.yml | 1 + spec/options_spec.rb | 36 +++++++++++++++++++++ 7 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/deep_merge3/.config1.yml.swp create mode 100644 spec/fixtures/deep_merge3/.config2.yml.swp create mode 100644 spec/fixtures/deep_merge3/config1.yml create mode 100644 spec/fixtures/deep_merge3/config2.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index c82e1485..c46e5ccd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ ## Unreleased -... +### New features + +* Add `merge_hash_arrays` as a configuration option ([#214](https://github.com/railsconfig/config/pull/214)) ## 1.7.1 diff --git a/README.md b/README.md index fd5ce6de..c1159e43 100644 --- a/README.md +++ b/README.md @@ -270,6 +270,7 @@ located at `config/initializers/config.rb`. ### Merge customization * `overwrite_arrays` - overwrite arrays found in previously loaded settings file. Default: `true` +* `merge_hash_arrays` - merge hashes inside of arrays from previously loaded settings files. Makes sense only when `overwrite_arrays = false`. Default: `false` * `knockout_prefix` - ability to remove elements of the array set in earlier loaded settings file. Makes sense only when `overwrite_arrays = false`, otherwise array settings would be overwritten by default. Default: `nil` * `merge_nil_values` - `nil` values will overwrite an existing value when merging configs. Default: `true`. diff --git a/spec/fixtures/deep_merge3/.config1.yml.swp b/spec/fixtures/deep_merge3/.config1.yml.swp new file mode 100644 index 0000000000000000000000000000000000000000..ea107b48caeed67f0d45b951d5b80099fdde55e3 GIT binary patch literal 12288 zcmeI%O-chX6u|LU_oaRvU>w(NT3e{}1}Ly@zlj zm9Ffpt^WrhUoV08+l5TKS7+zJurrVvh%lPpzbp<|U~57rr7s+rcs?OdBw_g4wM&T@WVe=!*dAh21$*`$5il>SMt z>l2T|qu^lwa`PE>5kLR|1Q0*~0R#|0V5VGr Squ}~6T8~CSxX5+*G?6cb$}*7v literal 0 HcmV?d00001 diff --git a/spec/fixtures/deep_merge3/.config2.yml.swp b/spec/fixtures/deep_merge3/.config2.yml.swp new file mode 100644 index 0000000000000000000000000000000000000000..7e0753b21bb3a444a46f42d62df638cd51cd91ba GIT binary patch literal 12288 zcmeI%Jx;?g6u|LU_6h_&KnOE+X)6`=1TZimSSUhC>V!zNjT~E0A>d5hgA=f^_YlOW z?M5?G{!g;}^=#?=Hp_A6`urjowEI%Up2+*>VAN_gbzo$e3t8 T3~rv2^(Y<&p?#c%FSqgyX2~+9 literal 0 HcmV?d00001 diff --git a/spec/fixtures/deep_merge3/config1.yml b/spec/fixtures/deep_merge3/config1.yml new file mode 100644 index 00000000..c277eeab --- /dev/null +++ b/spec/fixtures/deep_merge3/config1.yml @@ -0,0 +1 @@ +array: [{a: "one"}] diff --git a/spec/fixtures/deep_merge3/config2.yml b/spec/fixtures/deep_merge3/config2.yml new file mode 100644 index 00000000..413dac30 --- /dev/null +++ b/spec/fixtures/deep_merge3/config2.yml @@ -0,0 +1 @@ +array: [{b: "two"}] diff --git a/spec/options_spec.rb b/spec/options_spec.rb index 504f0150..cc156da7 100644 --- a/spec/options_spec.rb +++ b/spec/options_spec.rb @@ -179,4 +179,40 @@ expect(config.key?('existing')).to eq(false) end end + + context 'when merge_hash_arrays options' do + before { Config.reset } + + context 'is set to true' do + before { Config.setup { |cfg| + cfg.overwrite_arrays = false + cfg.merge_hash_arrays = true + } } + + it 'should merge the arrays' do + config = Config.load_files("#{fixture_path}/deep_merge3/config1.yml", "#{fixture_path}/deep_merge3/config2.yml") + + expect(config.array.length).to eq(1) + expect(config.array[0].a).to eq("one") + expect(config.array[0].b).to eq("two") + end + end + + context 'is set to false' do + before { Config.setup { |cfg| + cfg.overwrite_arrays = false + cfg.merge_hash_arrays = false + } } + + it 'should merge the arrays' do + config = Config.load_files("#{fixture_path}/deep_merge3/config1.yml", "#{fixture_path}/deep_merge3/config2.yml") + + expect(config.array.length).to eq(2) + expect(config.array[0].b).to eq(nil) + expect(config.array[1].b).to eq("two") + end + end + + end + end From cbb03a0581654d042eb25d4738df0e5ec21bbc64 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 10 Jan 2019 23:19:32 -0100 Subject: [PATCH 3/5] Remove tabs that somehow snuck in there --- spec/options_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/options_spec.rb b/spec/options_spec.rb index cc156da7..bc0e7025 100644 --- a/spec/options_spec.rb +++ b/spec/options_spec.rb @@ -185,9 +185,9 @@ context 'is set to true' do before { Config.setup { |cfg| - cfg.overwrite_arrays = false - cfg.merge_hash_arrays = true - } } + cfg.overwrite_arrays = false + cfg.merge_hash_arrays = true + } } it 'should merge the arrays' do config = Config.load_files("#{fixture_path}/deep_merge3/config1.yml", "#{fixture_path}/deep_merge3/config2.yml") @@ -200,9 +200,9 @@ context 'is set to false' do before { Config.setup { |cfg| - cfg.overwrite_arrays = false - cfg.merge_hash_arrays = false - } } + cfg.overwrite_arrays = false + cfg.merge_hash_arrays = false + } } it 'should merge the arrays' do config = Config.load_files("#{fixture_path}/deep_merge3/config1.yml", "#{fixture_path}/deep_merge3/config2.yml") From f419dfaad880c8ab3dcb19c7e3a14302615032d0 Mon Sep 17 00:00:00 2001 From: Piotr Kuczynski Date: Fri, 21 Jun 2019 00:06:10 +0200 Subject: [PATCH 4/5] Delete .config1.yml.swp --- spec/fixtures/deep_merge3/.config1.yml.swp | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 spec/fixtures/deep_merge3/.config1.yml.swp diff --git a/spec/fixtures/deep_merge3/.config1.yml.swp b/spec/fixtures/deep_merge3/.config1.yml.swp deleted file mode 100644 index ea107b48caeed67f0d45b951d5b80099fdde55e3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI%O-chX6u|LU_oaRvU>w(NT3e{}1}Ly@zlj zm9Ffpt^WrhUoV08+l5TKS7+zJurrVvh%lPpzbp<|U~57rr7s+rcs?OdBw_g4wM&T@WVe=!*dAh21$*`$5il>SMt z>l2T|qu^lwa`PE>5kLR|1Q0*~0R#|0V5VGr Squ}~6T8~CSxX5+*G?6cb$}*7v From 1fd683d4454448c1cc0971b36112ec1132e8dff6 Mon Sep 17 00:00:00 2001 From: Piotr Kuczynski Date: Fri, 21 Jun 2019 00:06:19 +0200 Subject: [PATCH 5/5] Delete .config2.yml.swp --- spec/fixtures/deep_merge3/.config2.yml.swp | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 spec/fixtures/deep_merge3/.config2.yml.swp diff --git a/spec/fixtures/deep_merge3/.config2.yml.swp b/spec/fixtures/deep_merge3/.config2.yml.swp deleted file mode 100644 index 7e0753b21bb3a444a46f42d62df638cd51cd91ba..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI%Jx;?g6u|LU_6h_&KnOE+X)6`=1TZimSSUhC>V!zNjT~E0A>d5hgA=f^_YlOW z?M5?G{!g;}^=#?=Hp_A6`urjowEI%Up2+*>VAN_gbzo$e3t8 T3~rv2^(Y<&p?#c%FSqgyX2~+9