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
# File lib/puppet/pops/merge_strategy.rb 363 def self.key 364 :deep 365 end
Protected Class Methods
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 393 def options_t 394 @options_t ||= Types::TypeParser.singleton.parse('Struct[{'\ 395 "strategy=>Optional[Pattern[#{key}]],"\ 396 'knockout_prefix=>Optional[String],'\ 397 'merge_debug=>Optional[Boolean],'\ 398 'merge_hash_arrays=>Optional[Boolean],'\ 399 'sort_merged_arrays=>Optional[Boolean],'\ 400 '}]') 401 end
Public Instance Methods
# File lib/puppet/pops/merge_strategy.rb 367 def checked_merge(e1, e2) 368 dm_options = { :preserve_unmergeables => false } 369 options.each_pair { |k,v| dm_options[k.to_sym] = v unless k == 'strategy' } 370 # e2 (the destination) is deep cloned to avoid that the passed in object mutates 371 DeepMerge.deep_merge!(e1, deep_clone(e2), dm_options) 372 end
# File lib/puppet/pops/merge_strategy.rb 374 def deep_clone(value) 375 if value.is_a?(Hash) 376 result = value.clone 377 value.each{ |k, v| result[k] = deep_clone(v) } 378 result 379 elsif value.is_a?(Array) 380 value.map{ |v| deep_clone(v) } 381 else 382 value 383 end 384 end
Protected Instance Methods
# File lib/puppet/pops/merge_strategy.rb 404 def value_t 405 @value_t ||= Types::PAnyType::DEFAULT 406 end