Class: TheArrayComparator::Comparator

Inherits:
StrategyDispatcher show all
Defined in:
lib/the_array_comparator/comparator.rb

Overview

the main comparator shell class

Instance Method Summary collapse

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

Parameters:

  • data (Array)

    the data which should be used as check, will be passed to the concrete comparator strategy

  • type (Symbol)

    the comparator strategy (needs to be registered first)

  • keywords (Array)

    what to look for in the data, will be passed to the concrete comparator strategy

  • options (Hash) (defaults to: {})

    exception, should not be considered as match

Options Hash (options):

  • exceptions (Hash)

    the exceptions from keywords

  • tag (String)

    a tag to identify the check

Raises:



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,options={})
  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 options

  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_methodsObject

See Also:

  • StrategyWrapper


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

Parameters:

  • number (Integer)

    the index of the check which should be deleted



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_checkObject

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_strategyObject

See Also:

  • StrategyWrapper


37
38
39
# File 'lib/the_array_comparator/comparator.rb', line 37

def exception_invalid_strategy
  Exceptions::IncompatibleComparator
end

#list_checksArray

List all added checks

Returns:

  • (Array)

    all available checks



130
131
132
# File 'lib/the_array_comparator/comparator.rb', line 130

def list_checks
  @cache[:checks].stored_objects
end

#resultResult

The result of all checks defined

Returns:

  • (Result)

    the result class with all the data need for further analysis



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

Returns:

  • (TrueClass, FalseClass)

    the result of all checks. if at least one fails the result will be ‘false’. If all are true, the result will be true.



105
106
107
# File 'lib/the_array_comparator/comparator.rb', line 105

def success?
  result.of_checks
end