heuristics 1.0.2 → 2.0.0
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.md +11 -15
- data/lib/heuristics.rb +0 -1
- data/lib/heuristics/builder.rb +14 -4
- data/lib/heuristics/tester.rb +1 -1
- data/lib/heuristics/version.rb +1 -1
- data/test/lib/heuristics/builder_test.rb +1 -1
- data/test/lib/heuristics/functional_test.rb +10 -10
- metadata +2 -3
- data/lib/heuristics/condition_evaluator.rb +0 -20
data/README.md
CHANGED
@@ -7,9 +7,9 @@ A typical simple example can look like this:
|
|
7
7
|
Heuristics.define(:field_tester) do
|
8
8
|
assume_default :integer
|
9
9
|
|
10
|
-
assume(:date) {
|
11
|
-
assume(:string) {
|
12
|
-
assume(:hash) {
|
10
|
+
assume(:date) { Chronic.parse(value) }
|
11
|
+
assume(:string) { value.instance_of? String }
|
12
|
+
assume(:hash) { value.instance_of? Hash }
|
13
13
|
end
|
14
14
|
|
15
15
|
Then you can use it like this
|
@@ -20,10 +20,6 @@ Then you can use it like this
|
|
20
20
|
# Returns :date
|
21
21
|
Heuristics.test(:field_tester, '23.09.1985')
|
22
22
|
|
23
|
-
# Falls back to :integer per assume_default. None of the other assumptions returned trus
|
24
|
-
Heuristics.test(:field_tester, [])
|
25
|
-
|
26
|
-
|
27
23
|
|
28
24
|
## Installation
|
29
25
|
|
@@ -52,16 +48,16 @@ This means you should write your assumptions in the order from most specific to
|
|
52
48
|
|
53
49
|
# Assummption with multiple conditions. Returns :hash_with_values
|
54
50
|
# if all conditions return true
|
55
|
-
assume(:hash_with_values)
|
56
|
-
|
57
|
-
|
51
|
+
assume(:hash_with_values) do
|
52
|
+
value.instance_of? Hash
|
53
|
+
value.keys.size > 0
|
58
54
|
}
|
59
55
|
|
60
56
|
# An assumption that will return :date if all conditions return true
|
61
|
-
assume(:date) {
|
57
|
+
assume(:date) { Chronic.parse(value) != nil }
|
62
58
|
|
63
59
|
# An assumption that will return :string if all conditions return true
|
64
|
-
assume(:string)
|
60
|
+
assume(:string) { value.instance_of? String }
|
65
61
|
end
|
66
62
|
|
67
63
|
|
@@ -76,9 +72,9 @@ This means you should write your assumptions in the order from most specific to
|
|
76
72
|
# Default value to return if no assumptions match
|
77
73
|
assume_default :set
|
78
74
|
|
79
|
-
assume(:date) {
|
80
|
-
assume(:string)
|
81
|
-
assume(:integer)
|
75
|
+
assume(:date) { Chronic.parse(value) != nil }
|
76
|
+
assume(:string) { value.instance_of? String }
|
77
|
+
assume(:integer) { value.instance_of? Fixnum }
|
82
78
|
end
|
83
79
|
|
84
80
|
Heuristics.test([1,2,3,'23.09.85','1','2','3','4']) # returns :string
|
data/lib/heuristics.rb
CHANGED
data/lib/heuristics/builder.rb
CHANGED
@@ -1,14 +1,24 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
1
3
|
module Heuristics
|
2
4
|
class Builder
|
3
|
-
attr_reader :
|
5
|
+
attr_reader :assumptions, :default
|
4
6
|
|
5
7
|
def initialize
|
6
|
-
@
|
8
|
+
@assumptions = {}
|
7
9
|
end
|
8
10
|
|
9
11
|
def assume(type, &block)
|
10
|
-
raise "An assumption with the name '#{type}' already exists" unless @
|
11
|
-
@
|
12
|
+
raise "An assumption with the name '#{type}' already exists" unless @assumptions[type].nil?
|
13
|
+
@assumptions[type] = Proc.new(&block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def check(value)
|
17
|
+
context = OpenStruct.new(value: value)
|
18
|
+
|
19
|
+
@assumptions.map do |type, prok|
|
20
|
+
(context.instance_eval(&prok) ? type : default) rescue default
|
21
|
+
end.reject(&:nil?).first
|
12
22
|
end
|
13
23
|
|
14
24
|
def assume_default(type)
|
data/lib/heuristics/tester.rb
CHANGED
@@ -7,7 +7,7 @@ module Heuristics
|
|
7
7
|
def test(value)
|
8
8
|
freq = Frequency.new
|
9
9
|
[*value].map do |v|
|
10
|
-
freq.add(@builder.
|
10
|
+
freq.add(@builder.check(v) || @builder.default)
|
11
11
|
end
|
12
12
|
|
13
13
|
freq.list.keys.first
|
data/lib/heuristics/version.rb
CHANGED
@@ -2,7 +2,7 @@ require_relative '../../test_helper'
|
|
2
2
|
|
3
3
|
describe Heuristics::Builder do
|
4
4
|
it 'should respond to #tests' do
|
5
|
-
Heuristics::Builder.new.must_respond_to :
|
5
|
+
Heuristics::Builder.new.must_respond_to :assumptions
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'must allow setting a default assumption' do
|
@@ -2,13 +2,13 @@ require_relative '../../test_helper'
|
|
2
2
|
|
3
3
|
describe Heuristics do
|
4
4
|
it 'should allow skipping the heuristics name' do
|
5
|
-
Heuristics.define { assume_default :string; assume(:email) {
|
5
|
+
Heuristics.define { assume_default :string; assume(:email) { false } }
|
6
6
|
Heuristics.test(1).must_equal :string
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'should allow to use external libs for testing' do
|
10
10
|
require 'chronic'
|
11
|
-
Heuristics.define(:external_lib_test) { assume(:date) {
|
11
|
+
Heuristics.define(:external_lib_test) { assume(:date) { Chronic.parse(value) != nil } }
|
12
12
|
Heuristics.test('23.09.85', :external_lib_test).must_equal :date
|
13
13
|
end
|
14
14
|
|
@@ -17,9 +17,9 @@ describe Heuristics do
|
|
17
17
|
Heuristics.define(:first_come_first_serve) do
|
18
18
|
assume_default nil # This is implicit anyway
|
19
19
|
|
20
|
-
assume(:date) {
|
21
|
-
assume(:integer_string) {
|
22
|
-
assume(:string) {
|
20
|
+
assume(:date) { Chronic.parse(value) != nil }
|
21
|
+
assume(:integer_string) { value =~ /\A\d+\Z/ }
|
22
|
+
assume(:string) { value.instance_of? String }
|
23
23
|
end
|
24
24
|
|
25
25
|
Heuristics.test('23.09.85', :first_come_first_serve).must_equal :date
|
@@ -40,8 +40,8 @@ describe Heuristics do
|
|
40
40
|
it 'should return the first true assumption' do
|
41
41
|
Heuristics.define(:assumption_test) do
|
42
42
|
assume_default :integer
|
43
|
-
assume(:string) {
|
44
|
-
assume(:hash) {
|
43
|
+
assume(:string) { value.instance_of? String }
|
44
|
+
assume(:hash) { value.instance_of? Hash }
|
45
45
|
end
|
46
46
|
Heuristics.test('abc', :assumption_test).must_equal :string
|
47
47
|
end
|
@@ -55,9 +55,9 @@ describe Heuristics do
|
|
55
55
|
require 'chronic'
|
56
56
|
|
57
57
|
Heuristics.define(:array_test) do
|
58
|
-
assume(:date) {
|
59
|
-
assume(:string) {
|
60
|
-
assume(:integer) {
|
58
|
+
assume(:date) { Chronic.parse(value) }
|
59
|
+
assume(:string) { value.instance_of? String }
|
60
|
+
assume(:integer) { value.instance_of? Fixnum }
|
61
61
|
end
|
62
62
|
|
63
63
|
Heuristics.test([1,2,3,'a','b','c','d'], :array_test).must_equal :string
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: heuristics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version:
|
5
|
+
version: 2.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Peter Haza
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -75,7 +75,6 @@ files:
|
|
75
75
|
- heuristics.gemspec
|
76
76
|
- lib/heuristics.rb
|
77
77
|
- lib/heuristics/builder.rb
|
78
|
-
- lib/heuristics/condition_evaluator.rb
|
79
78
|
- lib/heuristics/tester.rb
|
80
79
|
- lib/heuristics/version.rb
|
81
80
|
- test/lib/heuristics/builder_test.rb
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'ostruct'
|
2
|
-
|
3
|
-
module Heuristics
|
4
|
-
class ConditionEvaluator
|
5
|
-
|
6
|
-
def initialize
|
7
|
-
@conditions = []
|
8
|
-
end
|
9
|
-
|
10
|
-
def condition(&block)
|
11
|
-
@conditions << Proc.new(&block)
|
12
|
-
end
|
13
|
-
|
14
|
-
def check(value)
|
15
|
-
context = OpenStruct.new(value: value)
|
16
|
-
|
17
|
-
@conditions.map{|cond| context.instance_eval(&cond) rescue false}.reject{|v| v}.length == 0
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|