diff --git a/README.md b/README.md index 326d88ea..6f08f27c 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,7 @@ You can customize `Config` only once, preferably during application initializati Config.setup do |config| config.const_name = 'Settings' config.knockout_prefix = nil + config.overwrite_arrays = false end ``` @@ -271,6 +272,8 @@ Inheritance customization (check [Deep Merge](https://github.com/danielsdeleo/de * `knockout_prefix` - ability to remove elements of the array set in earlier loaded settings file. Default: `nil` +* `overwrite_arrays` - ability to replace an entire array set in earlier loaded settings file. Default: `false` + ## Working with Heroku Heroku uses ENV object to store sensitive settings which are like the local files described above. You cannot upload diff --git a/lib/config.rb b/lib/config.rb index faeab004..8a49e9f8 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -16,8 +16,9 @@ module Config @@use_env = false # deep_merge options - mattr_accessor :knockout_prefix + mattr_accessor :knockout_prefix, :overwrite_arrays @@knockout_prefix = nil + @@overwrite_arrays = false def self.setup yield self if @@_ran_once == false diff --git a/lib/config/options.rb b/lib/config/options.rb index efe8c290..279bec91 100644 --- a/lib/config/options.rb +++ b/lib/config/options.rb @@ -36,7 +36,10 @@ def reload_env! key.to_s.split('.').reverse.each do |element| hash = {element => hash} end - DeepMerge.deep_merge!(hash, conf, :preserve_unmergeables => false) + DeepMerge.deep_merge!(hash, + conf, + preserve_unmergeables: false, + overwrite_arrays: Config.overwrite_arrays) end merge!(conf[Config.const_name] || {}) @@ -57,7 +60,8 @@ def reload! DeepMerge.deep_merge!(source_conf, conf, preserve_unmergeables: false, - knockout_prefix: Config.knockout_prefix) + knockout_prefix: Config.knockout_prefix, + overwrite_arrays: Config.overwrite_arrays) end end @@ -101,7 +105,10 @@ def to_json(*args) def merge!(hash) current = to_hash - DeepMerge.deep_merge!(hash.dup, current) + DeepMerge.deep_merge!(hash.dup, + current, + preserve_unmergeables: false, + overwrite_arrays: Config.overwrite_arrays) marshal_load(__convert(current).marshal_dump) self end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 8d5fd3bb..3d51ddca 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -358,5 +358,37 @@ end end end + + context 'using overwrite_arrays' do + context 'in configuration phase' do + it 'should be able to assign a different overwrite_arrays value' do + Config.reset + Config.overwrite_arrays = true + + expect(Config.overwrite_arrays).to eq(true) + end + + it 'should have the default overwrite_arrays value equal false' do + Config.reset + + expect(Config.overwrite_arrays).to eq(false) + end + end + + context 'overwriting' do + let(:config) do + Config.overwrite_arrays = true + Config.load_files(["#{fixture_path}/overwrite_arrays/config1.yml", + "#{fixture_path}/overwrite_arrays/config2.yml", + "#{fixture_path}/overwrite_arrays/config3.yml"]) + end + + it 'should remove elements from settings' do + expect(config.array1).to eq(['item4', 'item5', 'item6']) + expect(config.array2.inner).to eq(['item4', 'item5', 'item6']) + expect(config.array3).to eq([]) + end + end + end end end diff --git a/spec/fixtures/overwrite_arrays/config1.yml b/spec/fixtures/overwrite_arrays/config1.yml new file mode 100644 index 00000000..f19bfa3f --- /dev/null +++ b/spec/fixtures/overwrite_arrays/config1.yml @@ -0,0 +1,17 @@ +array1: + - item1 + - item2 + - item3 + - item4 + +array2: + inner: + - item1 + - item2 + - item3 + - item4 + +array3: + - item1 + - item2 + - item3 diff --git a/spec/fixtures/overwrite_arrays/config2.yml b/spec/fixtures/overwrite_arrays/config2.yml new file mode 100644 index 00000000..f19b5cb8 --- /dev/null +++ b/spec/fixtures/overwrite_arrays/config2.yml @@ -0,0 +1,12 @@ +array1: + - item2 + - item4 + - item5 + +array2: + inner: + - item2 + - item4 + - item5 + +array3: [] diff --git a/spec/fixtures/overwrite_arrays/config3.yml b/spec/fixtures/overwrite_arrays/config3.yml new file mode 100644 index 00000000..85093ed7 --- /dev/null +++ b/spec/fixtures/overwrite_arrays/config3.yml @@ -0,0 +1,10 @@ +array1: + - item4 + - item5 + - item6 + +array2: + inner: + - item4 + - item5 + - item6 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ee5e69d1..2d2ac5d0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -43,9 +43,10 @@ # Extend Config module with ability to reset configuration to the default values def self.reset - self.const_name = 'Settings' - self.use_env = false - self.knockout_prefix = nil + self.const_name = 'Settings' + self.use_env = false + self.knockout_prefix = nil + self.overwrite_arrays = false end end end