rulebook 0.3.1 → 0.3.2
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 +134 -116
- data/rulebook.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.2
|
data/lib/rulebook.rb
CHANGED
@@ -1,116 +1,134 @@
|
|
1
|
-
class RuleBook
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
1
|
+
class RuleBook
|
2
|
+
class Rule
|
3
|
+
attr_accessor :what_to_capture, :block
|
4
|
+
|
5
|
+
def initialize(what_to_capture, &block)
|
6
|
+
raise(TypeError, 'what_to_capture must be of type Regexp') unless what_to_capture.is_a?(Regexp)
|
7
|
+
@what_to_capture, @block = what_to_capture, block
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches_against?(query)
|
11
|
+
!match_against(query).nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def match_against(query)
|
15
|
+
query.to_s.match(@what_to_capture)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class RuleBook
|
21
|
+
attr_accessor :rules
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
@rules = []
|
25
|
+
end
|
26
|
+
|
27
|
+
def rule(what_to_capture, &block)
|
28
|
+
rule = Rule.new(what_to_capture, &block)
|
29
|
+
@rules << rule
|
30
|
+
rule
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_rules_that_match_against(query)
|
34
|
+
@rules.find_all { |rule| !query.to_s.match(rule.what_to_capture).nil? }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class RuleBook
|
39
|
+
module IncludeMethods
|
40
|
+
def respond_to?(meth)
|
41
|
+
rulebook = self.class.const_get('INSTANCE_RULEBOOK')
|
42
|
+
rulebook.find_rules_that_match_against(meth).any? || super
|
43
|
+
end
|
44
|
+
|
45
|
+
def method_missing(meth, *args, &block)
|
46
|
+
rulebook = self.class.const_get('INSTANCE_RULEBOOK')
|
47
|
+
rules = rulebook.find_rules_that_match_against(meth)
|
48
|
+
|
49
|
+
unless rules.nil? || rules.empty?
|
50
|
+
rule = rules.first
|
51
|
+
captures = rule.match_against(meth).captures || []
|
52
|
+
block = rule.block
|
53
|
+
arity = block.arity == -1 ? 0 : block.arity
|
54
|
+
self.class.send(:define_method, meth) do |*args|
|
55
|
+
instance_exec(*(captures + args).take(arity), &block)
|
56
|
+
end
|
57
|
+
send(meth, *args, &block)
|
58
|
+
else
|
59
|
+
super
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
module ExtendMethods
|
65
|
+
def respond_to?(meth)
|
66
|
+
rulebook = const_get('CLASS_NOTEBOOK')
|
67
|
+
rulebook.find_rules_that_match_against(meth).any? || super
|
68
|
+
end
|
69
|
+
|
70
|
+
def method_missing(meth, *args, &block)
|
71
|
+
rulebook = const_get('CLASS_NOTEBOOK')
|
72
|
+
rules = rulebook.find_rules_that_match_against(meth)
|
73
|
+
|
74
|
+
unless rules.nil?
|
75
|
+
rule = rules.first
|
76
|
+
captures = rule.match_against(meth).captures || []
|
77
|
+
block = rule.block
|
78
|
+
arity = block.arity == -1 ? 0 : block.arity
|
79
|
+
klass = class << self; self; end
|
80
|
+
klass.send(:define_method, meth) do |*args|
|
81
|
+
class_exec(*(captures + args).take(arity), &block)
|
82
|
+
end
|
83
|
+
send(meth, *args, &block)
|
84
|
+
else
|
85
|
+
super
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class Module
|
92
|
+
def rule(what_to_capture, &block)
|
93
|
+
raise(ArgumentError, 'rules must have a block') unless block_given?
|
94
|
+
|
95
|
+
setup_rulebook('INSTANCE_RULEBOOK', :include)
|
96
|
+
const_get('INSTANCE_RULEBOOK').rule(what_to_capture, &block)
|
97
|
+
end
|
98
|
+
|
99
|
+
def class_rule(what_to_capture, &block)
|
100
|
+
raise(ArgumentError, 'class_rules must have a block') unless block_given?
|
101
|
+
|
102
|
+
setup_rulebook('CLASS_NOTEBOOK', :extend)
|
103
|
+
const_get('CLASS_NOTEBOOK').rule(what_to_capture, &block)
|
104
|
+
end
|
105
|
+
|
106
|
+
def rules(&block)
|
107
|
+
raise(ArgumentError, 'rules must have a block') unless block_given?
|
108
|
+
|
109
|
+
setup_rulebook('INSTANCE_RULEBOOK', :include)
|
110
|
+
const_get('INSTANCE_RULEBOOK').instance_eval(&block)
|
111
|
+
const_get('INSTANCE_RULEBOOK')
|
112
|
+
end
|
113
|
+
|
114
|
+
def class_rules(&block)
|
115
|
+
raise(ArgumentError, 'class_rules must have a block') unless block_given?
|
116
|
+
|
117
|
+
setup_rulebook('CLASS_NOTEBOOK', :extend)
|
118
|
+
const_get('CLASS_NOTEBOOK').instance_eval(&block)
|
119
|
+
const_get('CLASS_NOTEBOOK')
|
120
|
+
end
|
121
|
+
|
122
|
+
private
|
123
|
+
|
124
|
+
def setup_rulebook(rulebook_constant, extend_or_include)
|
125
|
+
raise(ArgumentError, 'extend_or_include must be :extend or :include') unless [:extend, :include].include?(extend_or_include)
|
126
|
+
|
127
|
+
unless const_defined?(rulebook_constant)
|
128
|
+
const_set(rulebook_constant, RuleBook.new)
|
129
|
+
|
130
|
+
module_name = extend_or_include.to_s.capitalize + 'Methods'
|
131
|
+
send(extend_or_include, RuleBook.const_get(module_name))
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
data/rulebook.gemspec
CHANGED