Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merge_hash_arrays as an option to pass to DeepMerge #214

Merged
merged 6 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* Dropped support for Rails `< 4.2`
* Dropped support for Ruby `< 2.4`

### New features

* Add `merge_hash_arrays` as a configuration option ([#214](https://github.com/railsconfig/config/pull/214))

### Changes

* Upgraded dry-validation dependency to dry-schema 1.0 ([#224](https://github.com/railsconfig/config/pull/224))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,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`.

Expand Down
3 changes: 2 additions & 1 deletion lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/config/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/deep_merge3/config1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
array: [{a: "one"}]
1 change: 1 addition & 0 deletions spec/fixtures/deep_merge3/config2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
array: [{b: "two"}]
36 changes: 36 additions & 0 deletions spec/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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