array_logic 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -29,4 +29,23 @@ The *match* and *matches* methods allow arrays to be tested against these rules:
29
29
  rule_two.match([a1, a2]) --> false
30
30
  rule_two.matches([a1, a2], [a1]) --> [[a1]]
31
31
 
32
- See test/array_logic/rule_test for more examples
32
+ See test/array_logic/rule_test for more examples
33
+
34
+ === Combinations that match
35
+
36
+ Two methods allow you to determine sample combinations that match the current
37
+ rule.
38
+
39
+ rule = ArrayLogic::Rule.new
40
+ rule.rule = 'a1 and a2'
41
+ rule.combinations_that_match --> [[1,2]]
42
+ rule.combinations_that_do_not_match --> [[1],[2]]
43
+
44
+ To limit the number of samples presented, both only use ids used within
45
+ the rule. For the example about, an array that includes [1,2] would match,
46
+ and so would [1,2,3]. However, arrays that only contain 1 or 2 would not match
47
+ (for example [1,3])
48
+
49
+ Run example.rb to see some more examples
50
+
51
+ ruby /lib/example.rb
@@ -1,7 +1,7 @@
1
1
  module ArrayLogic
2
2
  class Rule
3
3
  attr_accessor :rule
4
- attr_reader :things
4
+ attr_reader :things, :thing_ids
5
5
 
6
6
  def initialize
7
7
 
@@ -14,6 +14,7 @@ module ArrayLogic
14
14
  def match(things)
15
15
  rule_valid?
16
16
  @things = things
17
+ @thing_ids = things.collect(&:id)
17
18
  logic
18
19
  end
19
20
 
@@ -29,19 +30,43 @@ module ArrayLogic
29
30
  def replace_item(pattern, processor)
30
31
  @processed_rule = processed_rule.gsub(pattern) {|x| processor.call(x)}
31
32
  end
33
+
34
+ def object_ids_used
35
+ chrs_after_first = 1..-1
36
+ objects_identifiers_in_rule.collect{|i| i[chrs_after_first].to_i}
37
+ end
38
+
39
+ def combinations_that_match
40
+ combinations_of_identifiers_in_rule.delete_if{|ids| ! match_ids(ids)}
41
+ end
42
+
43
+ def combinations_that_do_not_match
44
+ combinations_of_identifiers_in_rule.delete_if{|ids| match_ids(ids)}
45
+ end
32
46
 
33
47
  private
34
- def thing_ids
35
- things.collect(&:id)
48
+ def match_ids(ids)
49
+ rule_valid?
50
+ @thing_ids = ids
51
+ logic
36
52
  end
37
-
53
+
54
+ def combinations_of_identifiers_in_rule
55
+ ids = object_ids_used
56
+ combinations = Array.new
57
+ (1..ids.length).each{|n| ids.combination(n).each{|c| combinations << c}}
58
+ return combinations
59
+ end
60
+
61
+ def objects_identifiers_in_rule
62
+ rule_without_punctuation.split.delete_if{|x| !(thing_id_pattern =~ x)}
63
+ end
64
+
38
65
  def expression
39
66
  rule_processing_steps
40
67
  return final_processed_rule
41
68
  end
42
69
 
43
-
44
-
45
70
  def rule_processing_steps
46
71
  add_space_around_puctuation_characters
47
72
  make_everything_lower_case
@@ -100,6 +125,10 @@ module ArrayLogic
100
125
  def reset_processed_rule_ready_for_next_comparison
101
126
  @processed_rule = nil
102
127
  end
128
+
129
+ def rule_without_punctuation
130
+ rule.gsub(/[[:punct:]]/, '')
131
+ end
103
132
 
104
133
  def check_rule_entered
105
134
  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.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/example.rb ADDED
@@ -0,0 +1,25 @@
1
+ require_relative 'array_logic'
2
+
3
+ rule_sets = [
4
+ "(t1 and t2) or (t3 and t4)",
5
+ "t1 and not t2",
6
+ "2 in t1 t2 t3",
7
+ "(2 in t1 t2 t3) and (1 in t4 t5)"
8
+ ]
9
+
10
+ rule_sets.each do |rule_set|
11
+
12
+ rule = ArrayLogic::Rule.new
13
+ rule.rule = rule_set
14
+
15
+ puts "----------------------------\n"
16
+ puts "The rule '#{rule_set}' would match the following:"
17
+
18
+ rule.combinations_that_match.each{|c| puts "\t#{c.inspect}"}
19
+
20
+ puts "\nAnd would not match"
21
+
22
+ rule.combinations_that_do_not_match.each{|c| puts "\t#{c.inspect}"}
23
+
24
+ puts "\n\n"
25
+ end
@@ -49,7 +49,7 @@ module ArrayLogic
49
49
  assert_no_thing_match([3], @rule)
50
50
  end
51
51
 
52
- def test_one_or_one_and
52
+ def test_one_or_one_and_version_two
53
53
  @rule.rule = '( t1 or t2 ) and t3'
54
54
  assert_no_thing_match([1, 2], @rule)
55
55
  assert_thing_match([1, 2, 3], @rule)
@@ -210,5 +210,36 @@ module ArrayLogic
210
210
  assert_equal(expected, result)
211
211
  end
212
212
 
213
+ def test_match_with_empty_rule
214
+ @rule.rule = ""
215
+ things = get_things([1, 2])
216
+ assert(!@rule.match([things.first]), "Should be no match when rule empty")
217
+ end
218
+
219
+ def test_matches_with_empty_rule
220
+ @rule.rule = ""
221
+ things = get_things([1, 2])
222
+ assert_equal([], @rule.matches([things.first], [things.last]))
223
+ end
224
+
225
+ def test_object_ids_used
226
+ @rule.rule = '(t1 && 1 in t2 t3) || t4'
227
+ assert_equal([1, 2, 3, 4], @rule.object_ids_used)
228
+ end
229
+
230
+ def test_combinations_that_match
231
+ @rule.rule = 't1 or t2'
232
+ assert_equal([[1], [2], [1,2]], @rule.combinations_that_match)
233
+ @rule.rule = 't1 and t2'
234
+ assert_equal([[1,2]], @rule.combinations_that_match)
235
+ end
236
+
237
+ def test_combinations_that_do_not_match
238
+ @rule.rule = 't1 or t2'
239
+ assert_equal([], @rule.combinations_that_do_not_match)
240
+ @rule.rule = 't1 and t2'
241
+ assert_equal([[1],[2]], @rule.combinations_that_do_not_match)
242
+ end
243
+
213
244
  end
214
245
  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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-26 00:00:00.000000000 Z
12
+ date: 2012-11-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Allow a user to define a set of rules, and then test to see if an array
15
15
  of object match those rules.
@@ -19,16 +19,17 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
- - lib/array_logic.rb
23
- - lib/array_logic/thing.rb
24
22
  - lib/array_logic/version.rb
23
+ - lib/array_logic/thing.rb
25
24
  - lib/array_logic/rule.rb
25
+ - lib/array_logic.rb
26
+ - lib/example.rb
26
27
  - MIT-LICENSE
27
28
  - Rakefile
28
29
  - README.rdoc
29
- - test/array_logic/thing_test.rb
30
- - test/array_logic/rule_test.rb
31
30
  - test/array_logic/test_case.rb
31
+ - test/array_logic/rule_test.rb
32
+ - test/array_logic/thing_test.rb
32
33
  homepage: https://github.com/reggieb/array_logic
33
34
  licenses: []
34
35
  post_install_message:
@@ -54,6 +55,6 @@ signing_key:
54
55
  specification_version: 3
55
56
  summary: Matches arrays of objects against logical rules.
56
57
  test_files:
57
- - test/array_logic/thing_test.rb
58
- - test/array_logic/rule_test.rb
59
58
  - test/array_logic/test_case.rb
59
+ - test/array_logic/rule_test.rb
60
+ - test/array_logic/thing_test.rb