Class: TheArrayComparator::StrategyDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/the_array_comparator/strategy_dispatcher.rb

Overview

the main comparator shell class

Direct Known Subclasses

Cache, Comparator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStrategyDispatcher

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

Parameters:

  • name (String, Symbol)

    the name for the reader

Raises:



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_methodsObject

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

Parameters:

  • block (Block)

    the block to be executed for each strategy

Returns:

  • (Enumerator)

    enumerator over all available 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_strategyObject

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

Parameters:

  • name (String, Symbol)

    The name which can be used to refer to the registered strategy

  • klass (Comparator)

    The strategy class which should be registered

Raises:

  • user defined exception Raise exception if an incompatible strategy class is given. Please see #exception_to_raise_for_invalid_strategy for more information



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