array_logic 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -38,8 +38,8 @@ rule.
38
38
 
39
39
  rule = ArrayLogic::Rule.new
40
40
  rule.rule = 'a1 and a2'
41
- rule.combinations_that_match --> [[1,2]]
42
- rule.combinations_that_do_not_match --> [[1],[2]]
41
+ rule.matching_combinations --> [[1,2]]
42
+ rule.blocking_combinations --> [[1],[2]]
43
43
 
44
44
  To limit the number of samples presented, both only use ids used within
45
45
  the rule. For the example about, an array that includes [1,2] would match,
@@ -12,7 +12,7 @@ module ArrayLogic
12
12
  end
13
13
 
14
14
  def match(things)
15
- rule_valid?
15
+ check_rule
16
16
  @things = things
17
17
  @thing_ids = things.collect(&:id)
18
18
  logic
@@ -23,8 +23,10 @@ module ArrayLogic
23
23
  end
24
24
 
25
25
  def rule_valid?
26
- check_rule_entered
27
- check_allowed_characters
26
+ check_rule
27
+ !rule.empty?
28
+ rescue
29
+ return false
28
30
  end
29
31
 
30
32
  def replace_item(pattern, processor)
@@ -33,24 +35,35 @@ module ArrayLogic
33
35
 
34
36
  def object_ids_used
35
37
  chrs_after_first = 1..-1
36
- objects_identifiers_in_rule.collect{|i| i[chrs_after_first].to_i}.uniq
38
+ @object_ids_used ||= objects_identifiers_in_rule.collect{|i| i[chrs_after_first].to_i}.uniq
37
39
  end
38
40
 
39
- def combinations_that_match
40
- combinations_of_identifiers_in_rule.delete_if{|ids| ! match_ids(ids)} if rule and !rule.empty?
41
+ def matching_combinations
42
+ combinations_of_identifiers_in_rule_that_pass {|c| match_ids(c)}
41
43
  end
42
44
 
43
- def combinations_that_do_not_match
44
- combinations_of_identifiers_in_rule.delete_if{|ids| match_ids(ids)} if rule and !rule.empty?
45
+ def blocking_combinations
46
+ combinations_of_identifiers_in_rule_that_pass {|c| ! match_ids(c)}
45
47
  end
46
48
 
47
49
  private
48
50
  def match_ids(ids)
49
- rule_valid?
50
51
  @thing_ids = ids
51
52
  logic
52
53
  end
53
54
 
55
+ def combinations_of_identifiers_in_rule_that_pass(&test)
56
+ if rule_valid?
57
+ combinations = Array.new
58
+ (1..id_count).each{|n| object_ids_used.combination(n).each{|c| combinations << c if test.call(c)}}
59
+ return combinations
60
+ end
61
+ end
62
+
63
+ def id_count
64
+ object_ids_used.length
65
+ end
66
+
54
67
  def combinations_of_identifiers_in_rule
55
68
  ids = object_ids_used
56
69
  combinations = Array.new
@@ -129,6 +142,11 @@ module ArrayLogic
129
142
  def rule_without_punctuation
130
143
  rule.gsub(/[[:punct:]]/, '')
131
144
  end
145
+
146
+ def check_rule
147
+ check_rule_entered
148
+ check_allowed_characters
149
+ end
132
150
 
133
151
  def check_rule_entered
134
152
  raise "You must define a rule before trying to match" unless rule.kind_of? String
@@ -1,3 +1,3 @@
1
1
  module ArrayLogic
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/example.rb CHANGED
@@ -15,11 +15,23 @@ rule_sets.each do |rule_set|
15
15
  puts "----------------------------\n"
16
16
  puts "The rule '#{rule_set}' would match the following:"
17
17
 
18
- rule.combinations_that_match.each{|c| puts "\t#{c.inspect}"}
18
+ rule.matching_combinations.each{|c| puts "\t#{c.inspect}"}
19
19
 
20
20
  puts "\nAnd would not match"
21
21
 
22
- rule.combinations_that_do_not_match.each{|c| puts "\t#{c.inspect}"}
22
+ rule.blocking_combinations.each{|c| puts "\t#{c.inspect}"}
23
23
 
24
24
  puts "\n\n"
25
25
  end
26
+
27
+ or_rule = (1..12).to_a.collect{|n| "t#{n}"}.join(" or ")
28
+
29
+ rule = ArrayLogic::Rule.new
30
+ rule.rule = or_rule
31
+
32
+ require 'benchmark'
33
+
34
+ Benchmark.bm do |x|
35
+ x.report(:matching_combinations) { rule.matching_combinations }
36
+ x.report(:blocking_combinations) { rule.blocking_combinations}
37
+ end
@@ -232,42 +232,42 @@ module ArrayLogic
232
232
  assert_equal([1, 2], @rule.object_ids_used)
233
233
  end
234
234
 
235
- def test_combinations_that_match
235
+ def test_matching_combinations
236
236
  @rule.rule = 't1 or t2'
237
- assert_equal([[1], [2], [1,2]], @rule.combinations_that_match)
237
+ assert_equal([[1], [2], [1,2]], @rule.matching_combinations)
238
238
  @rule.rule = 't1 and t2'
239
- assert_equal([[1,2]], @rule.combinations_that_match)
239
+ assert_equal([[1,2]], @rule.matching_combinations)
240
240
  end
241
241
 
242
- def test_combinations_that_match_without_rule
242
+ def test_matching_combinations_without_rule
243
243
  @rule.rule = nil
244
- assert_nil(@rule.combinations_that_match, "combinations_that_match should return nil if there is no rule")
244
+ assert_equal(nil, @rule.matching_combinations)
245
245
  @rule.rule = ""
246
- assert_nil(@rule.combinations_that_match, "combinations_that_match should return nil if the rule is blank")
246
+ assert_equal(nil, @rule.matching_combinations)
247
247
  end
248
248
 
249
- def test_combinations_that_match_with_duplicate_ids
249
+ def test_matching_combinations_with_duplicate_ids
250
250
  @rule.rule = 't1 and t2 and (2 in t1 t2)'
251
- assert_equal(1, @rule.combinations_that_match.length, "should not identify combinations for both occurancies of each id")
251
+ assert_equal(1, @rule.matching_combinations.length, "should not identify combinations for both occurancies of each id")
252
252
  end
253
253
 
254
- def test_combinations_that_do_not_match
254
+ def test_blocking_combinations
255
255
  @rule.rule = 't1 or t2'
256
- assert_equal([], @rule.combinations_that_do_not_match)
256
+ assert_equal([], @rule.blocking_combinations)
257
257
  @rule.rule = 't1 and t2'
258
- assert_equal([[1],[2]], @rule.combinations_that_do_not_match)
258
+ assert_equal([[1],[2]], @rule.blocking_combinations)
259
259
  end
260
260
 
261
- def test_combinations_that_do_not_match_without_rule
261
+ def test_blocking_combinations_without_rule
262
262
  @rule.rule = nil
263
- assert_nil(@rule.combinations_that_do_not_match, "combinations_that_do_not_match should return nil if there is no rule")
263
+ assert_equal(nil, @rule.blocking_combinations)
264
264
  @rule.rule = ""
265
- assert_nil(@rule.combinations_that_do_not_match, "combinations_that_do_not_match should return nil if the rule is blank")
265
+ assert_equal(nil, @rule.blocking_combinations)
266
266
  end
267
267
 
268
- def test_combinations_that_do_not_match_when_none_returned
268
+ def test_blocking_combinations_when_none_returned
269
269
  @rule.rule = 't1'
270
- assert_equal([], @rule.combinations_that_do_not_match)
270
+ assert_equal([], @rule.blocking_combinations)
271
271
  end
272
272
 
273
273
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array_logic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: