Class: Puppet::Pops::DeepMergeStrategy
- Inherits:
-
MergeStrategy
- Object
- MergeStrategy
- Puppet::Pops::DeepMergeStrategy
- Defined in:
- lib/puppet/pops/merge_strategy.rb
Overview
Documentation copied from github.com/danielsdeleo/deep_merge/blob/master/lib/deep_merge/core.rb altered with respect to preserve_unmergeables since this implementation always disables that option.
The destination is dup’ed before the deep_merge is called to allow frozen objects as values.
deep_merge method permits merging of arbitrary child elements. The two top level elements must be hashes. These hashes can contain unlimited (to stack limit) levels of child elements. These child elements to not have to be of the same types. Where child elements are of the same type, deep_merge will attempt to merge them together. Where child elements are not of the same type, deep_merge will skip or optionally overwrite the destination element with the contents of the source element at that level. So if you have two hashes like this:
source = {:x => [1,2,3], :y => 2}
dest = {:x => [4,5,'6'], :y => [7,8,9]}
dest.deep_merge!(source)
Results: {:x => [1,2,3,4,5,'6'], :y => 2}
“deep_merge” will unconditionally overwrite any unmergeables and merge everything else.
Options:
Options are specified in the last parameter passed, which should be in hash format:
hash.deep_merge!({:x => [1,2]}, {:knockout_prefix => '--'})
- 'knockout_prefix' Set to string value to signify prefix which deletes elements from existing element. Defaults is _undef_
- 'sort_merged_arrays' Set to _true_ to sort all arrays that are merged together. Default is _false_
- 'unpack_arrays' Set to string value used as a deliminator to join all array values and then split them again. Default is _undef_
- 'merge_hash_arrays' Set to _true_ to merge hashes within arrays. Default is _false_
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 => ""}
:unpack_arrays => The purpose of this is to permit compound elements to be passed
in as strings and to be converted into discrete array elements
irsource = {:x => ['1,2,3', '4']}
dest = {:x => ['5','6','7,8']}
dest.deep_merge!(source, {:unpack_arrays => ','})
Results: {:x => ['1','2','3','4','5','6','7','8']}
Why: If receiving data from an HTML form, this makes it easy for a checkbox
to pass multiple values from within a single HTML element
:merge_hash_arrays => merge hashes within arrays
source = {:x => [{:y => 1}]}
dest = {:x => [{:z => 2}]}
dest.deep_merge!(source, {:merge_hash_arrays => true})
Results: {:x => [{:y => 1, :z => 2}]}
Constant Summary
Constants inherited from MergeStrategy
MergeStrategy::NOT_FOUND, MergeStrategy::TypeAsserter, MergeStrategy::TypeParser
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from MergeStrategy
add_strategy, #configuration, #convert_value, #initialize, merge, #merge, #merge_lookup, #options, strategy, strategy_keys
Constructor Details
This class inherits a constructor from Puppet::Pops::MergeStrategy
Class Method Details
.key ⇒ Object
317 318 319 |
# File 'lib/puppet/pops/merge_strategy.rb', line 317 def self.key :deep end |
Instance Method Details
#checked_merge(e1, e2) ⇒ Object
321 322 323 324 325 326 |
# File 'lib/puppet/pops/merge_strategy.rb', line 321 def checked_merge(e1, e2) = { :preserve_unmergeables => false } .each_pair { |k,v| [k.to_sym] = v unless k == 'strategy' } # e2 (the destination) is dup'ed to avoid that the passed in object mutates DeepMerge.deep_merge!(e1, e2.dup, ) end |