class Puppet::Pops::DeepMergeStrategy

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

Constants

INSTANCE

Public Class Methods

key() click to toggle source
    # File lib/puppet/pops/merge_strategy.rb
364 def self.key
365   :deep
366 end

Protected Class Methods

options_t() click to toggle source

Returns a type that allows all deep_merge options except 'preserve_unmergeables' since we force the setting of that option to false

@return [Types::PAnyType] the puppet type used when validating the options hash

    # File lib/puppet/pops/merge_strategy.rb
394 def options_t
395   @options_t ||= Types::TypeParser.singleton.parse('Struct[{'\
396                                                    "strategy=>Optional[Pattern[#{key}]],"\
397                                                    'knockout_prefix=>Optional[String],'\
398                                                    'merge_debug=>Optional[Boolean],'\
399                                                    'merge_hash_arrays=>Optional[Boolean],'\
400                                                    'sort_merged_arrays=>Optional[Boolean],'\
401                                                    '}]')
402 end

Public Instance Methods

checked_merge(e1, e2) click to toggle source
    # File lib/puppet/pops/merge_strategy.rb
368 def checked_merge(e1, e2)
369   dm_options = { :preserve_unmergeables => false }
370   options.each_pair { |k,v| dm_options[k.to_sym] = v unless k == 'strategy' }
371   # e2 (the destination) is deep cloned to avoid that the passed in object mutates
372   DeepMerge.deep_merge!(e1, deep_clone(e2), dm_options)
373 end
deep_clone(value) click to toggle source
    # File lib/puppet/pops/merge_strategy.rb
375 def deep_clone(value)
376   if value.is_a?(Hash)
377     result = value.clone
378     value.each{ |k, v| result[k] = deep_clone(v) }
379     result
380   elsif value.is_a?(Array)
381     value.map{ |v| deep_clone(v) }
382   else
383     value
384   end
385 end

Protected Instance Methods

value_t() click to toggle source
    # File lib/puppet/pops/merge_strategy.rb
405 def value_t
406   @value_t ||= Types::PAnyType::DEFAULT
407 end