Class: TheArrayComparator::Comparator
- Inherits:
-
StrategyDispatcher
- Object
- StrategyDispatcher
- TheArrayComparator::Comparator
- Defined in:
- lib/the_array_comparator/comparator.rb
Overview
the main comparator shell class
Instance Method Summary collapse
-
#add_check(data, type, keywords, options = {}) ⇒ Object
Add a check to test against.
- #class_must_have_methods ⇒ Object
-
#delete_check(number) ⇒ Object
Delete check.
-
#delete_last_check ⇒ Object
Delete the last check added.
- #exception_invalid_strategy ⇒ Object
-
#initialize(cache = Cache.new) ⇒ Comparator
constructor
Create a new comparator instance and register default comparators.
-
#list_checks ⇒ Array
List all added checks.
-
#result ⇒ Result
The result of all checks defined.
-
#success? ⇒ TrueClass, FalseClass
Run all checks.
Methods inherited from StrategyDispatcher
#each, #exception_to_raise_for_invalid_strategy, #register, strategy_reader
Constructor Details
#initialize(cache = Cache.new) ⇒ Comparator
Create a new comparator instance and register default comparators
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/the_array_comparator/comparator.rb', line 17 def initialize(cache=Cache.new) super() @cache = cache @cache.add(:checks, :anonymous_cache) @cache.add(:result, :single_value_cache) register :contains_all, SearchingStrategies::ContainsAll register :contains_any, SearchingStrategies::ContainsAny register :not_contains, SearchingStrategies::ContainsNot register :contains_all_as_substring, SearchingStrategies::ContainsAllWithSubstringSearch register :contains_any_as_substring, SearchingStrategies::ContainsAnyWithSubstringSearch register :not_contains_substring, SearchingStrategies::ContainsNotWithSubstringSearch register :is_equal, SearchingStrategies::IsEqual register :is_not_equal, SearchingStrategies::IsNotEqual @result = Result.new end |
Instance Method Details
#add_check(data, type, keywords, options = {}) ⇒ Object
Add a check to test against
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/the_array_comparator/comparator.rb', line 70 def add_check(data,type,keywords,={}) t = type.to_sym raise Exceptions::UnknownCheckType, "Unknown check type \":#{t}\" given. Did you register it in advance?" unless comparators.has_key?(t) opts = { exceptions: [], tag:'', }.merge sample = Sample.new(data,keywords,opts[:exceptions],opts[:tag]) strategy_klass = comparators[t] check = Check.new(strategy_klass,sample) @cache[:checks].add check end |
#class_must_have_methods ⇒ Object
42 43 44 45 46 |
# File 'lib/the_array_comparator/comparator.rb', line 42 def class_must_have_methods [ :success?, ] end |
#delete_check(number) ⇒ Object
Delete check
113 114 115 116 117 118 119 |
# File 'lib/the_array_comparator/comparator.rb', line 113 def delete_check(number) if @cache[:checks].fetch_object(number) @cache[:checks].delete_object(number) else raise Exceptions::CheckDoesNotExist, "You tried to delete a check, which does not exist!" end end |
#delete_last_check ⇒ Object
Delete the last check added
122 123 124 |
# File 'lib/the_array_comparator/comparator.rb', line 122 def delete_last_check delete_check(-1) end |
#exception_invalid_strategy ⇒ Object
37 38 39 |
# File 'lib/the_array_comparator/comparator.rb', line 37 def exception_invalid_strategy Exceptions::IncompatibleComparator end |
#list_checks ⇒ Array
List all added checks
130 131 132 |
# File 'lib/the_array_comparator/comparator.rb', line 130 def list_checks @cache[:checks].stored_objects end |
#result ⇒ Result
The result of all checks defined
90 91 92 93 94 95 96 97 98 |
# File 'lib/the_array_comparator/comparator.rb', line 90 def result if @cache[:checks].new_objects? @cache[:checks].stored_objects.each do |c| @result = Result.new( c.sample ) unless c.success? end end @result end |
#success? ⇒ TrueClass, FalseClass
Run all checks
105 106 107 |
# File 'lib/the_array_comparator/comparator.rb', line 105 def success? result.of_checks end |