railsjedi/rails_config — pull request #66 by piccio (2013-12-19 10:31:09+00:00)
Title: add knockout_prefix constant to control deep merging behaviour

more configurable deep merging strategy, as shown in `lib/rails_config/vendor/deep_merge.rb` comments:
```ruby
module DeepMerge
[...]
# Options:
# Options are specified in the last parameter passed, which should be in hash format:
# hash.deep_merge!({:x => [1,2]}, {:knockout_prefix => '--'})
# :preserve_unmergeables DEFAULT: false
# Set to true to skip any unmergeable elements from source
# :knockout_prefix DEFAULT: nil
# Set to string value to signify prefix which deletes elements from existing element
# :sort_merged_arrays DEFAULT: false
# Set to true to sort all arrays that are merged together
# :unpack_arrays DEFAULT: nil
# Set to string value to run "Array::join" then "String::split" against all arrays
# :merge_debug DEFAULT: false
# Set to true to get console output of merge process for debugging
#
# Selected Options Details:
# :knockout_prefix => The purpose of this is to provide a way to remove elements
# from existing Hash by specifying them in a special way in incoming hash
# source = {:x => ['--1', '2']}
# dest = {:x => ['1', '3']}
# dest.ko_deep_merge!(source)
# Results: {:x => ['2','3']}
# Additionally, if the knockout_prefix is passed alone as a string, it will cause
# the entire element to be removed:
# source = {:x => '--'}
# dest = {:x => [1,2,3]}
# dest.ko_deep_merge!(source)
# Results: {:x => ""}
[...]
def DeepMerge.deep_merge!(source, dest, options = {})
[...]
```

You may customize the behaviour of deep merge by setting knockout_prefix in `config/initializers/rails_config.rb`:
```ruby
RailsConfig.setup do |config|
config.const_name = 'Settings'
config.knockout_prefix = '--'
end
```

in `settings.yml`:
```
array:
- item1
- item2
- item3
```

in `settings.local.yml`:
```
array:
- --item1
- --item2
- item4
```

```ruby
Settings.array # => ["item3", "item4"]
```