Class: Puppet::Pops::MergeStrategy
- Defined in:
- lib/puppet/pops/merge_strategy.rb
Overview
Merges to objects into one based on an implemented strategy.
Direct Known Subclasses
DeepMergeStrategy, FirstFoundStrategy, HashMergeStrategy, UniqueMergeStrategy
Constant Summary collapse
- NOT_FOUND =
Object.new.freeze
Class Method Summary collapse
-
.add_strategy(strategy_class) ⇒ Object
Adds a new merge strategy to the map of strategies known to this class.
- .key ⇒ Object
-
.merge(e1, e2, merge) ⇒ Object
Finds a merge strategy that corresponds to the given merge argument and delegates the task of merging the elements of e1 and e2 to it.
-
.strategy(merge) ⇒ MergeStrategy
Finds the merge strategy for the given merge, creates an instance of it and returns that instance.
-
.strategy_keys ⇒ Array<Symbol>
Returns the list of merge strategy keys known to this class.
Instance Method Summary collapse
- #configuration ⇒ Object
-
#convert_value(value) ⇒ Object
Converts a single value to the type expeced when peforming a merge of two elements.
-
#initialize(options) ⇒ MergeStrategy
constructor
Create a new instance of this strategy configured with the given options.
-
#merge(e1, e2) ⇒ Object
Merges the elements of e1 and e2 according to the rules of this strategy and options given when this instance was created.
-
#merge_lookup(lookup_variants) {|variant| ... } ⇒ Object
Merges the result of yielding the given lookup_variants to a given block.
- #options ⇒ Object
Constructor Details
#initialize(options) ⇒ MergeStrategy
Create a new instance of this strategy configured with the given options
81 82 83 84 |
# File 'lib/puppet/pops/merge_strategy.rb', line 81 def initialize() assert_type('The merge options', , ) = end |
Class Method Details
.add_strategy(strategy_class) ⇒ Object
Adds a new merge strategy to the map of strategies known to this class
57 58 59 60 61 62 63 |
# File 'lib/puppet/pops/merge_strategy.rb', line 57 def self.add_strategy(strategy_class) unless MergeStrategy > strategy_class raise ArgumentError, "MergeStrategies.add_strategy 'strategy_class' must be a 'MergeStrategy' class. Got #{strategy_class}" end strategies[strategy_class.key] = strategy_class nil end |
.key ⇒ Object
75 76 77 |
# File 'lib/puppet/pops/merge_strategy.rb', line 75 def self.key raise NotImplementedError, "Subclass must implement 'key'" end |
.merge(e1, e2, merge) ⇒ Object
Finds a merge strategy that corresponds to the given merge argument and delegates the task of merging the elements of e1 and e2 to it.
71 72 73 |
# File 'lib/puppet/pops/merge_strategy.rb', line 71 def self.merge(e1, e2, merge) strategy(merge).merge(e1, e2) end |
.strategy(merge) ⇒ MergeStrategy
Finds the merge strategy for the given merge, creates an instance of it and returns that instance.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/puppet/pops/merge_strategy.rb', line 26 def self.strategy(merge) return merge if merge.is_a?(MergeStrategy) merge = :first if merge.nil? Types::TypeAsserter.assert_instance_of("MergeStrategy 'merge' parameter", merge_t, merge) if merge.is_a?(Hash) merge_strategy = merge['strategy'] if merge_strategy.nil? raise ArgumentError, "The hash given as 'merge' must contain the name of a strategy in string form for the key 'strategy'" end = merge else merge_strategy = merge = {} end strategy = strategies[merge_strategy.to_sym] raise ArgumentError, "Unknown merge strategy: '#{merge_strategy}'" if strategy.nil? strategy.new() end |
Instance Method Details
#configuration ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/puppet/pops/merge_strategy.rb', line 137 def configuration if .nil? || .empty? self.class.key.to_s else .include?('strategy') ? : { 'strategy' => self.class.key.to_s }.merge() end end |
#convert_value(value) ⇒ Object
Converts a single value to the type expeced when peforming a merge of two elements
129 130 131 |
# File 'lib/puppet/pops/merge_strategy.rb', line 129 def convert_value(value) value end |
#merge(e1, e2) ⇒ Object
Merges the elements of e1 and e2 according to the rules of this strategy and options given when this instance was created
93 94 95 96 97 |
# File 'lib/puppet/pops/merge_strategy.rb', line 93 def merge(e1, e2) checked_merge( assert_type('The first element of the merge', value_t, e1), assert_type('The second element of the merge', value_t, e2)) end |
#merge_lookup(lookup_variants) {|variant| ... } ⇒ Object
Merges the result of yielding the given lookup_variants to a given block.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/puppet/pops/merge_strategy.rb', line 108 def merge_lookup(lookup_variants) result = lookup_variants.reduce(NOT_FOUND) do |memo, lookup_variant| not_found = true value = catch(:no_such_key) do v = yield(lookup_variant) not_found = false v end if not_found memo else memo.equal?(NOT_FOUND) ? convert_value(value) : merge(memo, value) end end throw :no_such_key if result == NOT_FOUND result end |