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.
-
.options_t ⇒ Types::PStructType
Returns the type used to validate the options hash.
-
.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 expected when merging two elements.
-
#initialize(options) ⇒ MergeStrategy
constructor
Create a new instance of this strategy configured with the given options.
-
#lookup(lookup_variants, lookup_invocation) {|variant, variant| ... } ⇒ Object
Merges the result of yielding the given lookup_variants to a given block.
-
#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) ⇒ Object deprecated Deprecated.
-
#merge_single(value) ⇒ Object
Applies the merge strategy on a single element.
- #options ⇒ Object
Constructor Details
#initialize(options) ⇒ MergeStrategy
Create a new instance of this strategy configured with the given options
83 84 85 86 |
# File 'lib/puppet/pops/merge_strategy.rb', line 83 def initialize() assert_type('The merge options', self.class., ) unless .empty? @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 64 65 |
# File 'lib/puppet/pops/merge_strategy.rb', line 57 def self.add_strategy(strategy_class) unless MergeStrategy > strategy_class # TRANSLATORS 'MergeStrategies.add_strategy' is a method, 'stratgey_class' is a variable and 'MergeStrategy' is a class name and should not be translated raise ArgumentError, _("MergeStrategies.add_strategy 'strategy_class' must be a 'MergeStrategy' class. Got %{strategy_class}") % { strategy_class: strategy_class } end strategies[strategy_class.key] = strategy_class nil end |
.key ⇒ Object
77 78 79 |
# File 'lib/puppet/pops/merge_strategy.rb', line 77 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.
73 74 75 |
# File 'lib/puppet/pops/merge_strategy.rb', line 73 def self.merge(e1, e2, merge) strategy(merge).merge(e1, e2) end |
.options_t ⇒ Types::PStructType
Returns the type used to validate the options hash
186 187 188 |
# File 'lib/puppet/pops/merge_strategy.rb', line 186 def @options_t ||= Types::TypeParser.singleton.parse("Struct[{strategy=>Optional[Pattern[/#{key}/]]}]") end |
.strategy(merge) ⇒ MergeStrategy
Finds the merge strategy for the given merge, creates an instance of it and returns that instance.
22 23 24 25 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 22 def self.strategy(merge) return DefaultMergeStrategy::INSTANCE unless merge return merge if merge.is_a?(MergeStrategy) if merge.is_a?(Hash) merge_strategy = merge['strategy'] if merge_strategy.nil? # TRANSLATORS 'merge' is a variable name and 'strategy' is a key and should not be translated raise ArgumentError, _("The hash given as 'merge' must contain the name of a strategy in string form for the key 'strategy'") end = merge.size == 1 ? EMPTY_HASH : merge else merge_strategy = merge = EMPTY_HASH end merge_strategy = merge_strategy.to_sym if merge_strategy.is_a?(String) strategy_class = strategies[merge_strategy] raise ArgumentError, _("Unknown merge strategy: '%{strategy}'") % { strategy: merge_strategy } if strategy_class.nil? == EMPTY_HASH ? strategy_class::INSTANCE : strategy_class.new() end |
.strategy_keys ⇒ Array<Symbol>
Returns the list of merge strategy keys known to this class
49 50 51 |
# File 'lib/puppet/pops/merge_strategy.rb', line 49 def self.strategy_keys strategies.keys - [:default, :unconstrained_deep, :reverse_deep] end |
Instance Method Details
#configuration ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/puppet/pops/merge_strategy.rb', line 171 def configuration if @options.nil? || @options.empty? self.class.key.to_s else @options.include?('strategy') ? @options : { 'strategy' => self.class.key.to_s }.merge(@options) end end |
#convert_value(value) ⇒ Object
Converts a single value to the type expected when merging two elements
156 157 158 |
# File 'lib/puppet/pops/merge_strategy.rb', line 156 def convert_value(value) value end |
#lookup(lookup_variants, lookup_invocation) {|variant, variant| ... } ⇒ Object
Merges the result of yielding the given lookup_variants to a given block.
Merges the result of yielding the given lookup_variants to a given block.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/puppet/pops/merge_strategy.rb', line 126 def lookup(lookup_variants, lookup_invocation) case lookup_variants.size when 0 throw :no_such_key when 1 merge_single(yield(lookup_variants[0])) else lookup_invocation.with(:merge, self) do 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 lookup_invocation.report_result(result) end end 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
95 96 97 98 99 100 |
# File 'lib/puppet/pops/merge_strategy.rb', line 95 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) ⇒ Object
TODO: API 5.0 Remove this method
104 105 106 |
# File 'lib/puppet/pops/merge_strategy.rb', line 104 def merge_lookup(lookup_variants) lookup(lookup_variants, Lookup::Invocation.current) end |
#merge_single(value) ⇒ Object
Applies the merge strategy on a single element. Only applicable for ‘unique`
163 164 165 |
# File 'lib/puppet/pops/merge_strategy.rb', line 163 def merge_single(value) value end |
#options ⇒ Object
167 168 169 |
# File 'lib/puppet/pops/merge_strategy.rb', line 167 def @options end |