Class: Puppet::Pops::DeepMergeStrategy

Inherits:
MergeStrategy show all
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_
- '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 => ""}

: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}]}

Direct Known Subclasses

UnconstrainedDeepMergeStrategy

Constant Summary collapse

INSTANCE =
self.new(EMPTY_HASH)

Constants inherited from MergeStrategy

MergeStrategy::NOT_FOUND

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MergeStrategy

add_strategy, #configuration, #convert_value, #initialize, #lookup, merge, #merge, #merge_lookup, #merge_single, #options, strategy, strategy_keys

Constructor Details

This class inherits a constructor from Puppet::Pops::MergeStrategy

Class Method Details

.keyObject



358
359
360
# File 'lib/puppet/pops/merge_strategy.rb', line 358

def self.key
  :deep
end

Instance Method Details

#checked_merge(e1, e2) ⇒ Object



362
363
364
365
366
367
# File 'lib/puppet/pops/merge_strategy.rb', line 362

def checked_merge(e1, e2)
  dm_options = { :preserve_unmergeables => false }
  options.each_pair { |k,v| dm_options[k.to_sym] = v unless k == 'strategy' }
  # e2 (the destination) is deep cloned to avoid that the passed in object mutates
  DeepMerge.deep_merge!(e1, deep_clone(e2), dm_options)
end

#deep_clone(value) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
# File 'lib/puppet/pops/merge_strategy.rb', line 369

def deep_clone(value)
  if value.is_a?(Hash)
    result = value.clone
    value.each{ |k, v| result[k] = deep_clone(v) }
    result
  elsif value.is_a?(Array)
    value.map{ |v| deep_clone(v) }
  else
    value
  end
end