heuristics 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +6 -1
- data/lib/heuristics/version.rb +1 -1
- data/test/lib/heuristics/functional_test.rb +15 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -66,7 +66,12 @@ Or install it yourself as:
|
|
66
66
|
# First assumption to test true for all conditions will win
|
67
67
|
# Returns :hash_with_values in this case
|
68
68
|
Heuristics.test(:field_tester, {a: 1})
|
69
|
-
|
69
|
+
|
70
|
+
|
71
|
+
NOTE:
|
72
|
+
Order matters. First assumption to return true for all conditions will be used.
|
73
|
+
This means you should write your assumptions in the order from most specific to least specfic.
|
74
|
+
|
70
75
|
## Contributing
|
71
76
|
|
72
77
|
1. Fork it
|
data/lib/heuristics/version.rb
CHANGED
@@ -33,6 +33,21 @@ describe Heuristics do
|
|
33
33
|
Heuristics.test(:external_lib_test, '23.09.85').must_equal :date
|
34
34
|
end
|
35
35
|
|
36
|
+
it 'should return the assumption that first matched all conditions' do
|
37
|
+
require 'chronic'
|
38
|
+
Heuristics.define(:first_come_first_serve) do
|
39
|
+
assume_default nil # This is implicit anyway
|
40
|
+
|
41
|
+
assume(:date) { condition { Chronic.parse(value) != nil } }
|
42
|
+
assume(:integer_string) { condition { value =~ /\A\d+\Z/ } }
|
43
|
+
assume(:string) { condition { value.instance_of? String } }
|
44
|
+
end
|
45
|
+
|
46
|
+
Heuristics.test(:first_come_first_serve, '23.09.85').must_equal :date
|
47
|
+
Heuristics.test(:first_come_first_serve, '27').must_equal :integer_string
|
48
|
+
Heuristics.test(:first_come_first_serve, 'This is string').must_equal :string
|
49
|
+
end
|
50
|
+
|
36
51
|
it 'should return default if no assumptions are true' do
|
37
52
|
Heuristics.define(:default_test) { assume_default :string; assume(:email) { condition { false } } }
|
38
53
|
Heuristics.test(:default_test, 1).must_equal :string
|