array_logic 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/array_logic/rule.rb +9 -1
- data/lib/array_logic/version.rb +5 -1
- data/test/array_logic/rule_test.rb +50 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -69,7 +69,7 @@ where:
|
|
69
69
|
|
70
70
|
[function] One of the functions listed below
|
71
71
|
[object_method] A method that can be called on all of the objects in the array
|
72
|
-
[operator] one of: <, <=, >, >=,
|
72
|
+
[operator] one of: <, <=, >, >=, ==, !=
|
73
73
|
[number] a number to compare with the result
|
74
74
|
|
75
75
|
Using this array as an example:
|
data/lib/array_logic/rule.rb
CHANGED
@@ -117,7 +117,7 @@ module ArrayLogic
|
|
117
117
|
end
|
118
118
|
|
119
119
|
def function_pattern
|
120
|
-
/(#{array_functions.keys.join('|')})\(\s*\:(\w+[\?\!]?)\s*\)\s*((==|[
|
120
|
+
/(#{array_functions.keys.join('|')})\(\s*\:(\w+[\?\!]?)\s*\)\s*((==|[\<\>\!]=?)\s*\d+(\.\d+)?)/
|
121
121
|
end
|
122
122
|
|
123
123
|
def array_functions
|
@@ -132,10 +132,18 @@ module ArrayLogic
|
|
132
132
|
lambda do |string|
|
133
133
|
all, array_function, function, operator = function_pattern.match(string).to_a
|
134
134
|
values = things.collect &function.to_sym
|
135
|
+
numbers_booleans_or_nils?(values)
|
135
136
|
result = values.instance_eval(array_functions[array_function.to_sym])
|
136
137
|
"( #{result} #{operator} )"
|
137
138
|
end
|
138
139
|
end
|
140
|
+
|
141
|
+
def numbers_booleans_or_nils?(values)
|
142
|
+
errors = values.reject{|v| v.nil? || v.is_a?(Numeric) || !!v == v}
|
143
|
+
unless errors.empty?
|
144
|
+
raise "Values must be numbers or nils. The problem is here: #{errors.join(", ")}"
|
145
|
+
end
|
146
|
+
end
|
139
147
|
|
140
148
|
def processed_rule
|
141
149
|
@processed_rule ||= rule.clone
|
data/lib/array_logic/version.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
module ArrayLogic
|
2
|
-
VERSION = "0.2.
|
2
|
+
VERSION = "0.2.1"
|
3
3
|
end
|
4
4
|
|
5
5
|
# History
|
6
6
|
# =======
|
7
7
|
#
|
8
|
+
# Version 0.2.1
|
9
|
+
# =============
|
10
|
+
# Improved handling of errors in functions and add != operator
|
11
|
+
#
|
8
12
|
# Version 0.2.0
|
9
13
|
# =============
|
10
14
|
# Adds functions: sum, count and average
|
@@ -238,6 +238,14 @@ module ArrayLogic
|
|
238
238
|
assert_no_thing_match([4], @rule)
|
239
239
|
end
|
240
240
|
|
241
|
+
def test_count_with_not_equal_operator
|
242
|
+
@rule.rule = 'count(:id_odd?) != 1'
|
243
|
+
assert_no_thing_match([1, 2], @rule)
|
244
|
+
assert_thing_match([1, 2, 3], @rule)
|
245
|
+
assert_no_thing_match([3], @rule)
|
246
|
+
assert_thing_match([4], @rule)
|
247
|
+
end
|
248
|
+
|
241
249
|
def test_function_with_other_rule
|
242
250
|
@rule.rule = 'sum(:id) >= 3 and t3'
|
243
251
|
assert_no_thing_match([1, 2], @rule)
|
@@ -254,6 +262,48 @@ module ArrayLogic
|
|
254
262
|
assert_no_thing_match([4], @rule)
|
255
263
|
end
|
256
264
|
|
265
|
+
def test_function_with_non_existent_object_method
|
266
|
+
@rule.rule = 'sum(:something) > 3'
|
267
|
+
assert_raise NoMethodError do
|
268
|
+
@rule.match(get_things([1, 2]))
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
def test_function_with_unsupported_function
|
273
|
+
@rule.rule = 'hat_stand(:id) > 3'
|
274
|
+
assert_raise RuntimeError do
|
275
|
+
@rule.match(get_things([1, 2]))
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_function_with_unsupported_operator
|
280
|
+
@rule.rule = 'sum(:id) || 3'
|
281
|
+
assert_raise RuntimeError do
|
282
|
+
@rule.match(get_things([1, 2]))
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_function_with_single_equals
|
287
|
+
@rule.rule = 'sum(:id) = 3'
|
288
|
+
assert_raise RuntimeError do
|
289
|
+
@rule.match(get_things([1, 2]))
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_function_without_number
|
294
|
+
@rule.rule = 'sum(:id) == a1'
|
295
|
+
assert_raise RuntimeError do
|
296
|
+
@rule.match(get_things([1, 2]))
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_function_with_object_method_that_does_not_return_number
|
301
|
+
@rule.rule = 'sum(:methods) == 1'
|
302
|
+
assert_raise RuntimeError do
|
303
|
+
@rule.match(get_things([1, 2]))
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
257
307
|
def test_match_without_rule
|
258
308
|
assert_raise RuntimeError do
|
259
309
|
@rule.match([1, 2])
|
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.2.
|
4
|
+
version: 0.2.1
|
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: 2013-03-
|
12
|
+
date: 2013-03-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.
|