rulebook 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +1 -1
- data/Gemfile +7 -0
- data/Gemfile.lock +21 -0
- data/LICENSE +1 -1
- data/README.md +41 -47
- data/Rakefile +33 -54
- data/VERSION +1 -1
- data/examples/simple.rb +43 -0
- data/lib/rulebook.rb +14 -153
- data/lib/rulebook/class_methods.rb +6 -0
- data/lib/rulebook/core_ext/module.rb +24 -0
- data/lib/rulebook/instance_methods.rb +41 -0
- data/lib/rulebook/rule.rb +18 -0
- data/rulebook.gemspec +49 -29
- data/test/helper.rb +10 -8
- data/test/test_chevy.rb +56 -69
- data/test/test_rule.rb +10 -0
- metadata +147 -54
- data/.gitignore +0 -21
- data/TODO +0 -1
- data/test/test_class_methods.rb +0 -53
- data/test/test_rules.rb +0 -90
- data/test/test_ryguy.rb +0 -45
- data/test/test_user.rb +0 -48
data/.gitignore
DELETED
data/test/test_class_methods.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestClassMethods < Test::Unit::TestCase
|
4
|
-
class Car
|
5
|
-
attr_accessor :make, :model
|
6
|
-
|
7
|
-
def initialize(make, model)
|
8
|
-
@make, @model = make, model
|
9
|
-
end
|
10
|
-
|
11
|
-
class_rules do
|
12
|
-
rule(/new_([a-z]+)_(.+)/) do |make, model|
|
13
|
-
|
14
|
-
make.capitalize!
|
15
|
-
model = model.split('_').inject(''){ |result, word|
|
16
|
-
result << word.capitalize + ' '
|
17
|
-
}.strip
|
18
|
-
new(make, model)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
should 'be a Ford F150' do
|
24
|
-
@car = Car.new_ford_f150
|
25
|
-
assert_equal @car.make, 'Ford'
|
26
|
-
assert_equal @car.model, 'F150'
|
27
|
-
end
|
28
|
-
|
29
|
-
should 'be a Ford Mustang' do
|
30
|
-
@car = Car.new_ford_mustang
|
31
|
-
assert_equal @car.make, 'Ford'
|
32
|
-
assert_equal @car.model, 'Mustang'
|
33
|
-
end
|
34
|
-
|
35
|
-
should 'be a Pontiac GTO' do
|
36
|
-
@car = Car.new_pontiac_gto
|
37
|
-
@car.model.upcase!
|
38
|
-
assert_equal @car.make, 'Pontiac'
|
39
|
-
assert_equal @car.model, 'GTO'
|
40
|
-
end
|
41
|
-
|
42
|
-
should 'be a Toyota Camry' do
|
43
|
-
@car = Car.new_toyota_camry
|
44
|
-
assert_equal @car.make, 'Toyota'
|
45
|
-
assert_equal @car.model, 'Camry'
|
46
|
-
end
|
47
|
-
|
48
|
-
should 'be a Hyundai Santa Fe' do
|
49
|
-
@car = Car.new_hyundai_santa_fe
|
50
|
-
assert_equal @car.make, 'Hyundai'
|
51
|
-
assert_equal 'Santa Fe', @car.model
|
52
|
-
end
|
53
|
-
end
|
data/test/test_rules.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'date'
|
3
|
-
|
4
|
-
class TestRule < Test::Unit::TestCase
|
5
|
-
class User
|
6
|
-
attr_accessor :name, :gender, :height, :eye_color, :date_of_birth, :age
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
class User
|
11
|
-
def User.list
|
12
|
-
@@list ||= []
|
13
|
-
end
|
14
|
-
|
15
|
-
def initialize
|
16
|
-
User.list << self
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# Instance Rules
|
21
|
-
class User
|
22
|
-
rule(/is_your_name_([a-z_]+)\?/) do |name|
|
23
|
-
@name.capitalize == name.capitalize
|
24
|
-
end
|
25
|
-
|
26
|
-
rules do
|
27
|
-
rule(/is_(male|female)\?/) do |gender|
|
28
|
-
@gender == gender.to_sym
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
User.rule(/was_born_([a-z]+_\d+(st|nd|rd|th)?_\d+)/) do |date_of_birth|
|
34
|
-
@date_of_birth = Date.parse(date_of_birth.gsub(/_/, ' '))
|
35
|
-
@age = (Date.today - @date_of_birth).to_i / 365
|
36
|
-
end
|
37
|
-
|
38
|
-
User.rules do
|
39
|
-
rule(/is_(\d+)\?/) do |age|
|
40
|
-
@age == age.to_i
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
# Class Rules
|
45
|
-
# TODO: two more
|
46
|
-
class User
|
47
|
-
class_rule(/new_(male|female)/) do |gender|
|
48
|
-
instance = new
|
49
|
-
instance.gender = gender.to_sym
|
50
|
-
instance
|
51
|
-
end
|
52
|
-
|
53
|
-
class_rules do
|
54
|
-
rule(/find_(males|females)/) do |gender|
|
55
|
-
list.find_all { |user| user.gender == gender.to_sym }
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
|
61
|
-
context 'Ryan' do
|
62
|
-
setup do
|
63
|
-
@user = User.new_male
|
64
|
-
@user.name = 'Ryan'
|
65
|
-
@user.was_born_jan_15_1991
|
66
|
-
end
|
67
|
-
|
68
|
-
should 'answer yes to all obvious questions' do
|
69
|
-
assert @user.is_your_name_ryan?
|
70
|
-
assert @user.is_male?
|
71
|
-
assert @user.is_19?
|
72
|
-
end
|
73
|
-
|
74
|
-
should 'answer no to all silly questions' do
|
75
|
-
refute @user.is_your_name_mark?
|
76
|
-
refute @user.is_female?
|
77
|
-
refute @user.is_21?
|
78
|
-
end
|
79
|
-
|
80
|
-
teardown do
|
81
|
-
@user = nil
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
context 'User list' do
|
86
|
-
should 'should have any females' do
|
87
|
-
assert User.find_females.length == 0
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
data/test/test_ryguy.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestRyguy < Test::Unit::TestCase
|
4
|
-
class Ryguy
|
5
|
-
attr_accessor :nouns, :adjectives
|
6
|
-
|
7
|
-
rules do
|
8
|
-
rule(/is_a_(.*)/) do |noun|
|
9
|
-
@nouns ||= []
|
10
|
-
@nouns << noun.gsub(/_/, ' ')
|
11
|
-
end
|
12
|
-
|
13
|
-
rule(/is_(.*)/) do |adjective|
|
14
|
-
@adjectives ||= []
|
15
|
-
@adjectives << adjective.gsub(/_/, ' ')
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'Ryguy' do
|
21
|
-
setup do
|
22
|
-
@ryguy = Ryguy.new
|
23
|
-
@ryguy.is_awesome
|
24
|
-
@ryguy.is_a_bear
|
25
|
-
@ryguy.is_superfly
|
26
|
-
@ryguy.is_a_programmer
|
27
|
-
@ryguy.is_fantastic
|
28
|
-
@ryguy.is_a_master_of_the_ancient_chinese_art_of_karate
|
29
|
-
end
|
30
|
-
|
31
|
-
should 'be awesome, superfly, and fantastic' do
|
32
|
-
assert_same_elements(
|
33
|
-
['awesome', 'superfly', 'fantastic'],
|
34
|
-
@ryguy.adjectives
|
35
|
-
)
|
36
|
-
end
|
37
|
-
|
38
|
-
should 'be a bear, a programmer, and a master of karate' do
|
39
|
-
assert_same_elements(
|
40
|
-
['bear', 'programmer', 'master of the ancient chinese art of karate'],
|
41
|
-
@ryguy.nouns
|
42
|
-
)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
data/test/test_user.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'date'
|
3
|
-
|
4
|
-
class TestUser < Test::Unit::TestCase
|
5
|
-
|
6
|
-
class User
|
7
|
-
attr_accessor :name, :gender, :height, :eye_color, :date_of_birth, :age
|
8
|
-
end
|
9
|
-
|
10
|
-
class User
|
11
|
-
rule(/is_(male|female)/) do |gender|
|
12
|
-
@gender = gender.to_sym
|
13
|
-
end
|
14
|
-
|
15
|
-
rules do
|
16
|
-
rule(/is_(\d+)_foot_(\d+)(_inches)?/) do |feet, inches|
|
17
|
-
@height = "#{feet}-#{inches}"
|
18
|
-
end
|
19
|
-
|
20
|
-
rule(/has_(amber|blue|brown|gray|grey|green|hazel|red)_eyes/) do |eye_color|
|
21
|
-
@eye_color = eye_color.to_sym
|
22
|
-
end
|
23
|
-
|
24
|
-
rule(/was_born_([a-z]+_\d+(st|nd|rd|th)_\d+)/) do |date_of_birth|
|
25
|
-
@date_of_birth = Date.parse(date_of_birth.gsub(/_/, ' '))
|
26
|
-
@age = (Date.today - @date_of_birth).to_i / 365
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context 'A User instance' do
|
32
|
-
setup do
|
33
|
-
@ryguy = User.new
|
34
|
-
@ryguy.is_male
|
35
|
-
@ryguy.is_5_foot_8
|
36
|
-
@ryguy.has_brown_eyes
|
37
|
-
@ryguy.was_born_january_15th_1991
|
38
|
-
end
|
39
|
-
|
40
|
-
should 'be valid' do
|
41
|
-
assert_equal :male, @ryguy.gender
|
42
|
-
assert_equal '5-8', @ryguy.height
|
43
|
-
assert_equal :brown, @ryguy.eye_color
|
44
|
-
assert_equal Date.parse('January 15th, 1991'), @ryguy.date_of_birth
|
45
|
-
assert_equal 19, @ryguy.age
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|