rulebook 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/rulebook.rb +41 -17
- data/rulebook.gemspec +2 -2
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.3
|
data/lib/rulebook.rb
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
class RuleBook
|
2
2
|
class Rule
|
3
|
-
|
3
|
+
attr :block
|
4
4
|
|
5
5
|
def initialize(what_to_capture, &block)
|
6
6
|
raise(TypeError, 'what_to_capture must be of type Regexp') unless what_to_capture.is_a?(Regexp)
|
7
7
|
@what_to_capture, @block = what_to_capture, block
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
|
10
|
+
def [](query)
|
11
|
+
query.to_s.match(@what_to_capture)
|
12
12
|
end
|
13
|
+
alias_method :match_against, :[]
|
13
14
|
|
14
|
-
def
|
15
|
-
query.
|
15
|
+
def matches_against?(query)
|
16
|
+
!self[query].nil?
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|
@@ -30,30 +31,42 @@ class RuleBook
|
|
30
31
|
rule
|
31
32
|
end
|
32
33
|
|
33
|
-
def
|
34
|
-
@rules.find_all { |rule|
|
34
|
+
def rules_that_match_against(regexp)
|
35
|
+
@rules.find_all { |rule| rule.matches_against?(regexp) }
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
39
|
+
# TODO: DRY up the code here a bit...
|
38
40
|
class RuleBook
|
39
41
|
module IncludeMethods
|
40
42
|
def respond_to?(meth)
|
41
43
|
rulebook = self.class.const_get('INSTANCE_RULEBOOK')
|
42
|
-
rulebook.
|
44
|
+
rulebook.rules_that_match_against(meth).any? || super
|
43
45
|
end
|
44
46
|
|
45
47
|
def method_missing(meth, *args, &block)
|
46
48
|
rulebook = self.class.const_get('INSTANCE_RULEBOOK')
|
47
|
-
rules = rulebook.
|
49
|
+
rules = rulebook.rules_that_match_against(meth)
|
48
50
|
|
51
|
+
# TODO: Why would this ever be nil?
|
49
52
|
unless rules.nil? || rules.empty?
|
50
|
-
|
51
|
-
|
53
|
+
# Run the first matched rule..
|
54
|
+
# TODO: if the method NEXT if called within the rule,
|
55
|
+
# then goto the next matched rule
|
56
|
+
rule = rules.first
|
57
|
+
captures = rule[meth].captures || []
|
52
58
|
block = rule.block
|
59
|
+
|
60
|
+
# Remove the possibility of optional arguments
|
53
61
|
arity = block.arity == -1 ? 0 : block.arity
|
54
|
-
|
62
|
+
|
63
|
+
# Define the method
|
64
|
+
klass = self.class
|
65
|
+
klass.send(:define_method, meth) do |*args|
|
55
66
|
instance_exec(*(captures + args).take(arity), &block)
|
56
67
|
end
|
68
|
+
|
69
|
+
# Call the method
|
57
70
|
send(meth, *args, &block)
|
58
71
|
else
|
59
72
|
super
|
@@ -64,22 +77,32 @@ class RuleBook
|
|
64
77
|
module ExtendMethods
|
65
78
|
def respond_to?(meth)
|
66
79
|
rulebook = const_get('CLASS_NOTEBOOK')
|
67
|
-
rulebook.
|
80
|
+
rulebook.rules_that_match_against(meth).any? || super
|
68
81
|
end
|
69
82
|
|
70
83
|
def method_missing(meth, *args, &block)
|
71
84
|
rulebook = const_get('CLASS_NOTEBOOK')
|
72
|
-
rules = rulebook.
|
85
|
+
rules = rulebook.rules_that_match_against(meth)
|
73
86
|
|
74
|
-
|
75
|
-
|
76
|
-
|
87
|
+
# TODO: Why would this ever be nil?
|
88
|
+
unless rules.nil? || rules.empty?
|
89
|
+
# Run the first matched rule..
|
90
|
+
# TODO: if the method NEXT if called within the rule,
|
91
|
+
# then goto the next matched rule
|
92
|
+
rule = rules.first
|
93
|
+
captures = rule[meth].captures || []
|
77
94
|
block = rule.block
|
95
|
+
|
96
|
+
# Remove the possibility of optional arguments
|
78
97
|
arity = block.arity == -1 ? 0 : block.arity
|
98
|
+
|
99
|
+
# Define the method
|
79
100
|
klass = class << self; self; end
|
80
101
|
klass.send(:define_method, meth) do |*args|
|
81
102
|
class_exec(*(captures + args).take(arity), &block)
|
82
103
|
end
|
104
|
+
|
105
|
+
# Call the method
|
83
106
|
send(meth, *args, &block)
|
84
107
|
else
|
85
108
|
super
|
@@ -88,6 +111,7 @@ class RuleBook
|
|
88
111
|
end
|
89
112
|
end
|
90
113
|
|
114
|
+
# TODO: DRY up the code here too...
|
91
115
|
class Module
|
92
116
|
def rule(what_to_capture, &block)
|
93
117
|
raise(ArgumentError, 'rules must have a block') unless block_given?
|
data/rulebook.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rulebook}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ryan Lewis"]
|
12
|
-
s.date = %q{2010-05-
|
12
|
+
s.date = %q{2010-05-10}
|
13
13
|
s.description = %q{Lets you define methods with regex for dynamic methods}
|
14
14
|
s.email = %q{c00lryguy@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 3
|
9
|
+
version: 0.3.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ryan Lewis
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-10 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|