the_array_comparator 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.rspec +2 -1
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +21 -21
  4. data/README.md +2 -2
  5. data/TODO.md +2 -1
  6. data/gemfiles/Gemfile.default +1 -1
  7. data/lib/the_array_comparator/cache.rb +43 -44
  8. data/lib/the_array_comparator/caching_strategies/anonymous_cache.rb +69 -0
  9. data/lib/the_array_comparator/caching_strategies/single_value_cache.rb +69 -0
  10. data/lib/the_array_comparator/comparator.rb +48 -40
  11. data/lib/the_array_comparator/exceptions.rb +31 -2
  12. data/lib/the_array_comparator/{strategies → searching_strategies}/base.rb +1 -1
  13. data/lib/the_array_comparator/{strategies → searching_strategies}/contains_all.rb +1 -1
  14. data/lib/the_array_comparator/{strategies → searching_strategies}/contains_all_with_substring_search.rb +1 -1
  15. data/lib/the_array_comparator/{strategies → searching_strategies}/contains_any.rb +1 -1
  16. data/lib/the_array_comparator/{strategies → searching_strategies}/contains_any_with_substring_search.rb +1 -1
  17. data/lib/the_array_comparator/{strategies → searching_strategies}/contains_not.rb +1 -1
  18. data/lib/the_array_comparator/{strategies → searching_strategies}/contains_not_with_substring_search.rb +1 -1
  19. data/lib/the_array_comparator/{strategies → searching_strategies}/is_equal.rb +1 -1
  20. data/lib/the_array_comparator/{strategies → searching_strategies}/is_not_equal.rb +1 -1
  21. data/lib/the_array_comparator/strategy_dispatcher.rb +110 -0
  22. data/lib/the_array_comparator/version.rb +2 -2
  23. data/lib/the_array_comparator.rb +12 -20
  24. data/spec/benchmark/benchmark_spec.rb +50 -9
  25. data/spec/cache/anonymous_cache_spec.rb +89 -0
  26. data/spec/cache/single_value_cache_spec.rb +77 -0
  27. data/spec/cache_spec.rb +21 -70
  28. data/spec/comparator/base_spec.rb +2 -2
  29. data/spec/comparator/comparator_spec.rb +7 -37
  30. data/spec/comparator/contains_all_spec.rb +7 -7
  31. data/spec/comparator/contains_all_with_substring_search_spec.rb +6 -6
  32. data/spec/comparator/contains_any_spec.rb +6 -6
  33. data/spec/comparator/contains_any_with_substring_search_spec.rb +5 -5
  34. data/spec/comparator/contains_not_spec.rb +6 -6
  35. data/spec/comparator/contains_not_with_substring_search_spec.rb +4 -4
  36. data/spec/comparator/is_equal_spec.rb +7 -7
  37. data/spec/comparator/is_not_equal_spec.rb +6 -6
  38. data/spec/spec_helper.rb +1 -1
  39. data/spec/strategy_dispatcher/strategy_dispatcher_spec.rb +159 -0
  40. metadata +20 -11
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --format Fuubar
2
- --t "~bm"
2
+ -t "~bm"
3
+ --order rand
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
data/Gemfile.lock CHANGED
@@ -1,11 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- the_array_comparator (0.1.1)
4
+ the_array_comparator (0.2.0)
5
5
  active_support
6
6
 
7
7
  GEM
8
- remote: http://rubygems.org/
8
+ remote: https://rubygems.org/
9
9
  specs:
10
10
  active_support (3.0.0)
11
11
  activesupport (= 3.0.0)
@@ -17,16 +17,16 @@ GEM
17
17
  rspec-expectations (>= 2.7.0)
18
18
  awesome_print (1.1.0)
19
19
  builder (3.1.4)
20
- childprocess (0.3.7)
21
- ffi (~> 1.0, >= 1.0.6)
20
+ childprocess (0.3.8)
21
+ ffi (~> 1.0, >= 1.0.11)
22
22
  chronic (0.9.0)
23
- churn (0.0.27)
23
+ churn (0.0.28)
24
24
  chronic (>= 0.2.3)
25
25
  hirb
26
26
  json_pure
27
27
  main
28
- ruby_parser (~> 3.1.0)
29
- sexp_processor (~> 4.1.0)
28
+ ruby_parser (~> 3.0)
29
+ sexp_processor (~> 4.1)
30
30
  coderay (1.0.8)
31
31
  columnize (0.3.6)
32
32
  cucumber (1.2.1)
@@ -34,37 +34,37 @@ GEM
34
34
  diff-lcs (>= 1.1.3)
35
35
  gherkin (~> 2.11.0)
36
36
  json (>= 1.4.6)
37
- debugger (1.3.0)
37
+ debugger (1.3.1)
38
38
  columnize (>= 0.3.1)
39
39
  debugger-linecache (~> 1.1.1)
40
- debugger-ruby_core_source (~> 1.1.7)
40
+ debugger-ruby_core_source (~> 1.1.8)
41
41
  debugger-linecache (1.1.2)
42
42
  debugger-ruby_core_source (>= 1.1.1)
43
- debugger-ruby_core_source (1.1.7)
43
+ debugger-ruby_core_source (1.1.8)
44
44
  diff-lcs (1.1.3)
45
45
  fattr (2.2.1)
46
46
  ffaker (1.15.0)
47
- ffi (1.3.1)
47
+ ffi (1.4.0)
48
48
  fuubar (1.1.0)
49
49
  rspec (~> 2.0)
50
50
  rspec-instafail (~> 0.2.0)
51
51
  ruby-progressbar (~> 1.0.0)
52
- gherkin (2.11.5)
53
- json (>= 1.4.6)
52
+ gherkin (2.11.6)
53
+ json (>= 1.7.6)
54
54
  github-markup (0.7.5)
55
55
  hashr (0.0.22)
56
56
  hirb (0.7.1)
57
- json (1.7.6)
58
- json_pure (1.7.6)
57
+ json (1.7.7)
58
+ json_pure (1.7.7)
59
59
  main (5.1.1)
60
60
  arrayfields (>= 4.7.4)
61
61
  chronic (>= 0.6.2)
62
62
  fattr (>= 2.2.0)
63
63
  map (>= 5.1.0)
64
- map (6.2.0)
64
+ map (6.3.0)
65
65
  method_source (0.8.1)
66
- multi_json (1.5.0)
67
- pry (0.9.11.4)
66
+ multi_json (1.6.1)
67
+ pry (0.9.12)
68
68
  coderay (~> 1.0.5)
69
69
  method_source (~> 0.8)
70
70
  slop (~> 3.4)
@@ -85,7 +85,7 @@ GEM
85
85
  ruby-progressbar (1.0.2)
86
86
  ruby_parser (3.1.1)
87
87
  sexp_processor (~> 4.1)
88
- sexp_processor (4.1.4)
88
+ sexp_processor (4.1.5)
89
89
  simplecov (0.7.1)
90
90
  multi_json (~> 1.0)
91
91
  simplecov-html (~> 0.7.1)
@@ -94,9 +94,9 @@ GEM
94
94
  thor (0.17.0)
95
95
  tmrb (1.2.7)
96
96
  thor
97
- travis-lint (1.5.0)
97
+ travis-lint (1.6.0)
98
98
  hashr (~> 0.0.22)
99
- yard (0.8.3)
99
+ yard (0.8.4.1)
100
100
 
101
101
  PLATFORMS
102
102
  ruby
data/README.md CHANGED
@@ -9,7 +9,7 @@ Can be used to compare to arrays with a consistent api.
9
9
 
10
10
  Add this line to your application's Gemfile:
11
11
 
12
- gem 'comparator'
12
+ gem 'the_array_comparator'
13
13
 
14
14
  And then execute:
15
15
 
@@ -17,7 +17,7 @@ And then execute:
17
17
 
18
18
  Or install it yourself as:
19
19
 
20
- $ gem install comparator
20
+ $ gem install the_array_comparator
21
21
 
22
22
  ## Usage
23
23
 
data/TODO.md CHANGED
@@ -1 +1,2 @@
1
- * Add clear (last) probe
1
+ * Cache -> only check if there are __no__ new objects
2
+
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
@@ -3,64 +3,63 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  #caching class
6
- class Cache
6
+ class Cache < StrategyDispatcher
7
7
 
8
- # Create cache
9
- def initialize
10
- @cache = []
11
- @new_objects = false
12
- end
8
+ strategy_reader :caching_strategies
9
+ attr_reader :caches
13
10
 
14
- # Add object to cache
15
- #
16
- # @param [Object] obj
17
- # the object which should be added to the cache
18
- #
19
- # @return [Object]
20
- # the object which has beed added
21
- def add(obj)
22
- @cache << obj
23
- @new_objects = true
11
+ # Variable to store available caches
12
+ def initialize
13
+ super()
14
+ @caches = {}
24
15
 
25
- obj
16
+ register :anonymous_cache, CachingStrategies::AnonymousCache
17
+ register :single_value_cache, CachingStrategies::SingleValueCache
26
18
  end
27
19
 
28
- # Return all stored objects
29
- #
30
- # @return [Array]
31
- # the cache
32
- def stored_objects
33
- @new_objects = false
34
- @cache
20
+ # @see [StrategyDispatcher]
21
+ def class_must_have_methods
22
+ [
23
+ :add,
24
+ :clear,
25
+ :stored_objects,
26
+ :new_objects?,
27
+ :delete_object,
28
+ :fetch_object,
29
+ ]
35
30
  end
36
31
 
37
- # Clear the cache (delete all objects)
38
- def clear
39
- @cache = []
32
+ # @see [StrategyDispatcher]
33
+ def exception_to_raise_for_invalid_strategy
34
+ Exceptions::IncompatibleCachingStrategy
40
35
  end
41
36
 
42
- # Are there new objects
37
+ # Retrieve cache
43
38
  #
44
- # @return [TrueClass,FalseClass]
45
- # the result of the check
46
- def new_objects?
47
- @new_objects
48
- end
39
+ # @param [Symbol] cache
40
+ # the cache to be used
41
+ def [](cache)
42
+ c = cache.to_sym
49
43
 
50
- # Delete an object from cache by number
51
- #
52
- # @return
53
- # the deleted object
54
- def delete_object(num)
55
- @cache.delete_at(num)
44
+ raise Exceptions::CacheDoesNotExist, "Unknown cache \":#{c}\" given. Did you create it in advance?" unless caches.has_key?(c)
45
+ caches[c]
56
46
  end
57
47
 
58
- # Request an object from cache by number
48
+ # Add a new cache
49
+ #
50
+ # @param [Symbol] cache
51
+ # the cache to be created
59
52
  #
60
- # @return
61
- # the requested object
62
- def fetch_object(num)
63
- @cache[num]
53
+ # @param [Symbol] strategy
54
+ # the cache strategy to be used
55
+ def add(cache,strategy)
56
+ c = cache.to_sym
57
+ s = strategy.to_sym
58
+
59
+ raise Exceptions::UnknownCachingStrategy, "Unknown caching strategy \":#{strategy}\" given. Did you register it in advance?" unless caching_strategies.has_key?(strategy)
60
+
61
+ caches[c] = caching_strategies[s].new
62
+ caches[c]
64
63
  end
65
64
  end
66
65
  end
@@ -0,0 +1,69 @@
1
+ #encoding: utf-8
2
+
3
+ # the main module
4
+ module TheArrayComparator
5
+ #caching strategies
6
+ module CachingStrategies
7
+ #anonymous cache
8
+ class AnonymousCache
9
+
10
+ # Create cache
11
+ def initialize
12
+ @cache = []
13
+ @new_objects = false
14
+ end
15
+
16
+ # Add object to cache
17
+ #
18
+ # @param [Object] obj
19
+ # the object which should be added to the cache
20
+ #
21
+ # @return [Object]
22
+ # the object which has beed added
23
+ def add(obj)
24
+ @cache << obj
25
+ @new_objects = true
26
+
27
+ obj
28
+ end
29
+
30
+ # Return all stored objects
31
+ #
32
+ # @return [Array]
33
+ # the cache
34
+ def stored_objects
35
+ @new_objects = false
36
+ @cache
37
+ end
38
+
39
+ # Clear the cache (delete all objects)
40
+ def clear
41
+ @cache = []
42
+ end
43
+
44
+ # Are there new objects
45
+ #
46
+ # @return [TrueClass,FalseClass]
47
+ # the result of the check
48
+ def new_objects?
49
+ @new_objects
50
+ end
51
+
52
+ # Delete an object from cache by number
53
+ #
54
+ # @return
55
+ # the deleted object
56
+ def delete_object(num)
57
+ @cache.delete_at(num)
58
+ end
59
+
60
+ # Request an object from cache by number
61
+ #
62
+ # @return
63
+ # the requested object
64
+ def fetch_object(num)
65
+ @cache[num]
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,69 @@
1
+ #encoding: utf-8
2
+
3
+ # the main module
4
+ module TheArrayComparator
5
+ #caching strategies
6
+ module CachingStrategies
7
+ #anonymous cache
8
+ class SingleValueCache
9
+
10
+ # Create cache
11
+ def initialize
12
+ @cache = []
13
+ @new_objects = false
14
+ end
15
+
16
+ # Add object to cache
17
+ #
18
+ # @param [Object] obj
19
+ # the object which should be added to the cache
20
+ #
21
+ # @return [Object]
22
+ # the object which has beed added
23
+ def add(obj)
24
+ @cache = [ obj ]
25
+ @new_objects = true
26
+
27
+ obj
28
+ end
29
+
30
+ # Return all stored objects
31
+ #
32
+ # @return [Array]
33
+ # the cache
34
+ def stored_objects
35
+ @new_objects = false
36
+ @cache
37
+ end
38
+
39
+ # Clear the cache (delete all objects)
40
+ def clear
41
+ @cache = []
42
+ end
43
+
44
+ # Are there new objects
45
+ #
46
+ # @return [TrueClass,FalseClass]
47
+ # the result of the check
48
+ def new_objects?
49
+ @new_objects
50
+ end
51
+
52
+ # Delete an object from cache by number
53
+ #
54
+ # @return
55
+ # the deleted object
56
+ def delete_object
57
+ @cache.delete_at(0)
58
+ end
59
+
60
+ # Request an object from cache by number
61
+ #
62
+ # @return
63
+ # the requested object
64
+ def fetch_object
65
+ @cache[0]
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,46 +1,48 @@
1
1
  #encoding: utf-8
2
+ require 'the_array_comparator/caching_strategies/anonymous_cache'
3
+ require 'the_array_comparator/caching_strategies/single_value_cache'
2
4
 
3
5
  # the main module
4
6
  module TheArrayComparator
5
7
  # the main comparator shell class
6
- class Comparator
7
-
8
- # VARIABLE to store strategies
9
- @comparators = {}
10
- class << self
11
-
12
- # @!attribute [rw] command
13
- # Return all available comparator strategies
14
- attr_accessor :comparators
15
-
16
- # Register a new comparator strategy
17
- #
18
- # @param [String,Symbol] name
19
- # The name which can be used to refer to the registered strategy
20
- #
21
- # @param [Comparator] klass
22
- # The strategy class which should be registered
23
- #
24
- # @raise Exceptions::IncompatibleComparator
25
- # Raise exception if an incompatible comparator class is given
26
- def register(name,klass)
27
- @comparators ||= {}
28
-
29
- if klass.new.respond_to?(:success?)
30
- @comparators[name.to_sym] = klass
31
- else
32
- raise Exceptions::IncompatibleComparator, "Registering #{klass} failed. It does not support \"success?\"-instance-method"
33
- end
34
- end
35
- end
8
+ class Comparator < StrategyDispatcher
9
+
10
+ strategy_reader :comparators
36
11
 
37
12
  # Create a new comparator instance
38
13
  # and register default comparators
39
14
  #
40
15
  # @return [Comparator]
41
16
  # a new comparator
42
- def initialize
43
- @cache = Cache.new
17
+ def initialize(cache=Cache.new)
18
+ super()
19
+
20
+ @cache = cache
21
+ @cache.add(:checks, :anonymous_cache)
22
+ @cache.add(:result, :single_value_cache)
23
+
24
+ register :contains_all, SearchingStrategies::ContainsAll
25
+ register :contains_any, SearchingStrategies::ContainsAny
26
+ register :not_contains, SearchingStrategies::ContainsNot
27
+ register :contains_all_as_substring, SearchingStrategies::ContainsAllWithSubstringSearch
28
+ register :contains_any_as_substring, SearchingStrategies::ContainsAnyWithSubstringSearch
29
+ register :not_contains_substring, SearchingStrategies::ContainsNotWithSubstringSearch
30
+ register :is_equal, SearchingStrategies::IsEqual
31
+ register :is_not_equal, SearchingStrategies::IsNotEqual
32
+
33
+ @result = Result.new
34
+ end
35
+
36
+ # @see StrategyWrapper
37
+ def exception_invalid_strategy
38
+ Exceptions::IncompatibleComparator
39
+ end
40
+
41
+ # @see StrategyWrapper
42
+ def class_must_have_methods
43
+ [
44
+ :success?,
45
+ ]
44
46
  end
45
47
 
46
48
  # Add a check to test against
@@ -66,17 +68,19 @@ module TheArrayComparator
66
68
  # @raise [Exceptions::UnknownCheckType]
67
69
  # if a unknown strategy is given (needs to be registered first)
68
70
  def add_check(data,type,keywords,options={})
69
- raise Exceptions::UnknownCheckType, "Unknown check type \":#{type}\" given. Did you register it in advance?" unless Comparator.comparators.has_key?(type)
71
+ t = type.to_sym
72
+
73
+ raise Exceptions::UnknownCheckType, "Unknown check type \":#{t}\" given. Did you register it in advance?" unless comparators.has_key?(t)
70
74
  opts = {
71
75
  exceptions: [],
72
76
  tag:'',
73
77
  }.merge options
74
78
 
75
79
  sample = Sample.new(data,keywords,opts[:exceptions],opts[:tag])
76
- strategy_klass = Comparator.comparators[type]
80
+ strategy_klass = comparators[t]
77
81
  check = Check.new(strategy_klass,sample)
78
82
 
79
- @cache.add check
83
+ @cache[:checks].add check
80
84
  end
81
85
 
82
86
  # The result of all checks defined
@@ -84,9 +88,13 @@ module TheArrayComparator
84
88
  # @return [Result]
85
89
  # the result class with all the data need for further analysis
86
90
  def result
87
- @cache.stored_objects.each { |c| return Result.new(c.sample) unless c.success? }
91
+ if @cache[:checks].new_objects?
92
+ @cache[:checks].stored_objects.each do |c|
93
+ @result = Result.new(c.sample) unless c.success?
94
+ end
95
+ end
88
96
 
89
- Result.new
97
+ @result
90
98
  end
91
99
 
92
100
  # Run all checks
@@ -103,8 +111,8 @@ module TheArrayComparator
103
111
  # @param [Integer] number
104
112
  # the index of the check which should be deleted
105
113
  def delete_check(number)
106
- if @cache.fetch_object(number)
107
- @cache.delete_object(number)
114
+ if @cache[:checks].fetch_object(number)
115
+ @cache[:checks].delete_object(number)
108
116
  else
109
117
  raise Exceptions::CheckDoesNotExist, "You tried to delete a check, which does not exist!"
110
118
  end
@@ -120,7 +128,7 @@ module TheArrayComparator
120
128
  # @return [Array]
121
129
  # all available checks
122
130
  def list_checks
123
- @cache.stored_objects
131
+ @cache[:checks].stored_objects
124
132
  end
125
133
  end
126
134
  end
@@ -3,19 +3,48 @@ module TheArrayComparator
3
3
  # exceptions which are going to be raised under
4
4
  # certain conditions
5
5
  module Exceptions
6
- # Used when one tries to add an unknown
7
- # probe type to check for
6
+ # Used if one tries to add an unknown
7
+ # check type
8
8
  class UnknownCheckType < Exception
9
9
  end
10
+ #
11
+ # Used if one tries to add an unknown
12
+ # caching strategy
13
+ class UnknownCachingStrategy < Exception
14
+ end
10
15
 
11
16
  # Used if one tries to register an
12
17
  # incompatible comparator
13
18
  class IncompatibleComparator < Exception
14
19
  end
20
+ #
21
+ # Used if one tries to register an
22
+ # incompatible caching strategy
23
+ class IncompatibleCachingStrategy < Exception
24
+ end
15
25
 
16
26
  # Used if one tries to delete an
17
27
  # unexisting probe
18
28
  class CheckDoesNotExist < Exception
19
29
  end
30
+
31
+ # Used if one tries to use an unexisting
32
+ # cache
33
+ class CacheDoesNotExist < Exception
34
+ end
35
+
36
+ # Used if one forgot to implement that
37
+ # method
38
+ class MustHaveMethodNotImplemented < Exception
39
+ end
40
+
41
+ # Used if one use the library the wrong way
42
+ class WrongUsageOfLibrary < Exception
43
+ end
44
+
45
+ # Use if one tries to register an internal
46
+ # keyword
47
+ class UsedInternalKeyword < Exception
48
+ end
20
49
  end
21
50
  end
@@ -3,7 +3,7 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  # the available strategies
6
- module Strategies
6
+ module SearchingStrategies
7
7
  #base class for strategies
8
8
  class Base
9
9
 
@@ -3,7 +3,7 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  # the available strategies
6
- module Strategies
6
+ module SearchingStrategies
7
7
  #strategy contains
8
8
  class ContainsAll < Base
9
9
 
@@ -3,7 +3,7 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  # the available strategies
6
- module Strategies
6
+ module SearchingStrategies
7
7
  # strategy contains substring
8
8
  class ContainsAllWithSubstringSearch < Base
9
9
 
@@ -3,7 +3,7 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  # the available strategies
6
- module Strategies
6
+ module SearchingStrategies
7
7
  #strategy contains
8
8
  class ContainsAny < Base
9
9
 
@@ -3,7 +3,7 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  # the available strategies
6
- module Strategies
6
+ module SearchingStrategies
7
7
  # strategy contains substring
8
8
  class ContainsAnyWithSubstringSearch < Base
9
9
 
@@ -3,7 +3,7 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  # the available strategies
6
- module Strategies
6
+ module SearchingStrategies
7
7
  #strategy contains
8
8
  class ContainsNot < Base
9
9
 
@@ -3,7 +3,7 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  # the available strategies
6
- module Strategies
6
+ module SearchingStrategies
7
7
  # strategy not contains substring
8
8
  class ContainsNotWithSubstringSearch < Base
9
9
 
@@ -3,7 +3,7 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  # the available strategies
6
- module Strategies
6
+ module SearchingStrategies
7
7
  #strategy is equal
8
8
  class IsEqual < Base
9
9
 
@@ -3,7 +3,7 @@
3
3
  # the main module
4
4
  module TheArrayComparator
5
5
  # the available strategies
6
- module Strategies
6
+ module SearchingStrategies
7
7
  #strategy is not equal
8
8
  class IsNotEqual < Base
9
9