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
Constant Summary collapse
- TypeAsserter =
Puppet::Pops::Types::TypeAsserter
- TypeParser =
Puppet::Pops::Types::TypeParser
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
-
#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 accoring to the implemented strategy and options given when this instance was created.
Constructor Details
#initialize(options) ⇒ MergeStrategy
Create a new instance of this strategy configured with the given options
80 81 82 83 |
# File 'lib/puppet/pops/merge_strategy.rb', line 80 def initialize() assert_type('merge_options', , ) @options = end |
Class Method Details
.add_strategy(strategy_class) ⇒ Object
Adds a new merge strategy to the map of strategies known to this class
56 57 58 59 60 61 62 |
# File 'lib/puppet/pops/merge_strategy.rb', line 56 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
74 75 76 |
# File 'lib/puppet/pops/merge_strategy.rb', line 74 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.
70 71 72 |
# File 'lib/puppet/pops/merge_strategy.rb', line 70 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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/puppet/pops/merge_strategy.rb', line 27 def self.strategy(merge) TypeAsserter.assert_instance_of("MergeStrategies.merge '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
#convert_value(value) ⇒ Object
Converts a single value to the type expeced when peforming a merge of two elements
101 102 103 |
# File 'lib/puppet/pops/merge_strategy.rb', line 101 def convert_value(value) value end |
#merge(e1, e2) ⇒ Object
Merges the elements of e1 and e2 accoring to the implemented strategy and options given when this instance was created
92 93 94 95 96 |
# File 'lib/puppet/pops/merge_strategy.rb', line 92 def merge(e1, e2) checked_merge( assert_type('e1', value_t, e1), assert_type('e2', value_t, e2)) end |