Class: TheArrayComparator::StrategyDispatcher
- Inherits:
-
Object
- Object
- TheArrayComparator::StrategyDispatcher
- Defined in:
- lib/the_array_comparator/strategy_dispatcher.rb
Overview
the main comparator shell class
Direct Known Subclasses
Class Method Summary collapse
-
.strategy_reader(name) ⇒ Object
Define a reader for the available strategies.
Instance Method Summary collapse
-
#class_must_have_methods ⇒ Object
Return all must have methods.
-
#each(&block) ⇒ Enumerator
Iterate over all strategies.
-
#exception_to_raise_for_invalid_strategy ⇒ Object
Returns the exception used in registration check.
-
#initialize ⇒ StrategyDispatcher
constructor
Create strategy dispatcher.
-
#register(name, klass) ⇒ Object
Register a new comparator strategy.
Constructor Details
#initialize ⇒ StrategyDispatcher
Create strategy dispatcher
9 10 11 |
# File 'lib/the_array_comparator/strategy_dispatcher.rb', line 9 def initialize @available_strategies = {} end |
Class Method Details
.strategy_reader(name) ⇒ Object
Define a reader for the available strategies
18 19 20 21 22 23 24 |
# File 'lib/the_array_comparator/strategy_dispatcher.rb', line 18 def strategy_reader(name) raise Exceptions::UsedInternalKeyword, "You tried to define a reader using an internal name , which is forbidden (your reader name: #{name}). Please choose another name. Thank you very much." if internal_keywords.include? name define_method name.to_sym do instance_variable_get :@available_strategies end end |
Instance Method Details
#class_must_have_methods ⇒ Object
Note:
has to be implemented by concrete dispatcher -> otherwise exception
Return all must have methods
75 76 77 |
# File 'lib/the_array_comparator/strategy_dispatcher.rb', line 75 def class_must_have_methods exception_if_not_implemented __method__ end |
#each(&block) ⇒ Enumerator
Iterate over all strategies
86 87 88 |
# File 'lib/the_array_comparator/strategy_dispatcher.rb', line 86 def each(&block) available_strategies.each(block) end |
#exception_to_raise_for_invalid_strategy ⇒ Object
Note:
has to be implemented by concrete dispatcher -> otherwise exception
Returns the exception used in registration check
67 68 69 |
# File 'lib/the_array_comparator/strategy_dispatcher.rb', line 67 def exception_to_raise_for_invalid_strategy exception_if_not_implemented __method__ end |
#register(name, klass) ⇒ Object
Register a new comparator strategy
55 56 57 58 59 60 61 |
# File 'lib/the_array_comparator/strategy_dispatcher.rb', line 55 def register(name,klass) if valid_strategy? klass available_strategies[name.to_sym] = klass else raise exception_to_raise_for_invalid_strategy, "Registering #{klass} failed. It does not support \"#{class_must_have_methods.join("-, ")}\"-method" end end |