Class: Puppet::Pops::UniqueMergeStrategy

Inherits:
MergeStrategy show all
Defined in:
lib/puppet/pops/merge_strategy.rb

Overview

Merges two values that must be either scalar or arrays into a unique set of values.

Scalar values will be converted into a one element arrays and array values will be flattened prior to forming the unique set. The order of the elements is preserved with e1 being the first contributor of elements and e2 the second.

Constant Summary collapse

INSTANCE =
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, #initialize, #lookup, merge, #merge, #merge_lookup, #options, options_t, strategy, strategy_keys

Constructor Details

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

Class Method Details

.keyObject



286
287
288
# File 'lib/puppet/pops/merge_strategy.rb', line 286

def self.key
  :unique
end

Instance Method Details

#checked_merge(e1, e2) ⇒ Array<Object>

Returns The unique set of elements.

Parameters:

  • e1 (Array<Object>)

    The first array

  • e2 (Array<Object>)

    The second array

Returns:

  • (Array<Object>)

    The unique set of elements



294
295
296
# File 'lib/puppet/pops/merge_strategy.rb', line 294

def checked_merge(e1, e2)
  convert_value(e1) | convert_value(e2)
end

#convert_value(e) ⇒ Object



298
299
300
# File 'lib/puppet/pops/merge_strategy.rb', line 298

def convert_value(e)
  e.is_a?(Array) ? e.flatten : [e]
end

#merge_single(value) ⇒ Object

If value is an array, then return the result of calling ‘uniq` on that array. Otherwise, the argument is returned.

Parameters:

  • value (Object)

    the value to merge with nothing

Returns:

  • (Object)

    the merged value



306
307
308
# File 'lib/puppet/pops/merge_strategy.rb', line 306

def merge_single(value)
  value.is_a?(Array) ? value.uniq : value
end