array_logic 0.1.1 → 0.1.2

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
@@ -1,9 +1,11 @@
1
1
  == array_logic
2
2
 
3
- A system that allows me to define the logic for comparing arrays.
3
+ A system that allows me to define the logic for comparing arrays of objects.
4
4
 
5
- The logic for an active record model Answer, looks like this:
5
+ One prerequisite for the comparison is that the objects have an id method that
6
+ returns a unique (within the set of objects) integer.
6
7
 
8
+ The logic for an active record model Answer, looks like this:
7
9
 
8
10
  a1 = Answer.find(1)
9
11
  a2 = Answer.find(2)
@@ -18,18 +18,13 @@ module ArrayLogic
18
18
  def match(things)
19
19
  check_rule
20
20
  @things = things
21
- @thing_ids = things.collect(&:id)
22
- logic
21
+ match_ids things.collect(&:id)
23
22
  end
24
23
 
25
24
  def block(things)
26
25
  ! match(things)
27
26
  end
28
27
 
29
- def logic
30
- eval(expression)
31
- end
32
-
33
28
  def rule_valid?
34
29
  check_rule
35
30
  !rule.empty?
@@ -37,10 +32,6 @@ module ArrayLogic
37
32
  return false
38
33
  end
39
34
 
40
- def replace_item(pattern, processor)
41
- @processed_rule = processed_rule.gsub(pattern) {|x| processor.call(x)}
42
- end
43
-
44
35
  def object_ids_used
45
36
  chrs_after_first = 1..-1
46
37
  @object_ids_used ||= objects_identifiers_in_rule.collect{|i| i[chrs_after_first].to_i}.uniq
@@ -60,6 +51,10 @@ module ArrayLogic
60
51
  end
61
52
 
62
53
  private
54
+ def logic
55
+ eval(expression)
56
+ end
57
+
63
58
  def match_ids(ids)
64
59
  @thing_ids = ids
65
60
  logic
@@ -90,9 +85,13 @@ module ArrayLogic
90
85
  add_space_around_puctuation_characters
91
86
  make_everything_lower_case
92
87
  replace_logic_words_with_operators
93
- replace_item(thing_id_pattern, true_or_false_for_thing_id_in_thing_ids)
88
+ replace_item(thing_id_pattern, ids_include_this_id)
94
89
  replace_item(number_in_set_pattern, comparison_of_number_with_true_count)
95
90
  end
91
+
92
+ def replace_item(pattern, processor)
93
+ @processed_rule = processed_rule.gsub(pattern) {|x| processor.call(x)}
94
+ end
96
95
 
97
96
  # for example: 2 in t1, t2, t3
98
97
  def number_in_set_pattern
@@ -112,7 +111,7 @@ module ArrayLogic
112
111
  /\w\d+/
113
112
  end
114
113
 
115
- def true_or_false_for_thing_id_in_thing_ids
114
+ def ids_include_this_id
116
115
  lambda {|s| thing_ids.include?(s[/\d+/].to_i)}
117
116
  end
118
117
 
@@ -1,3 +1,16 @@
1
1
  module ArrayLogic
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
4
+
5
+ # History
6
+ # =======
7
+ #
8
+ # Version 0.1.2
9
+ # =============
10
+ # Refactor to tidy up code and make private, rule methods that don't need exposing
11
+ #
12
+ #
13
+ # Version 0.1.1
14
+ # =============
15
+ # Working version. No history before this point
16
+ #
@@ -187,7 +187,7 @@ module ArrayLogic
187
187
  def test_replace_item
188
188
  @rule.rule = 't1 or ( t2 and t3 )'
189
189
  process = lambda {|s| [1, 2].include?(s[/\d+/].to_i)}
190
- result = @rule.replace_item(/\w\d+/, process)
190
+ result = @rule.send(:replace_item, /\w\d+/, process)
191
191
  assert_equal('true or ( true and false )', result)
192
192
  end
193
193
 
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.1.1
4
+ version: 0.1.2
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-12-13 00:00:00.000000000 Z
12
+ date: 2013-02-22 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.