rulebook 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/rulebook.rb +134 -116
  3. data/rulebook.gemspec +1 -1
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
data/lib/rulebook.rb CHANGED
@@ -1,116 +1,134 @@
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') \
7
- unless what_to_capture.is_a?(Regexp)
8
-
9
- @what_to_capture, @block = what_to_capture, block
10
- end
11
-
12
- def matches_against?(query)
13
- !match_against(query).nil?
14
- end
15
-
16
- def match_against(query)
17
- query.to_s.match(@what_to_capture)
18
- end
19
- end
20
- end
21
-
22
- class RuleBook
23
- attr_accessor :rules
24
-
25
- def initialize
26
- @rules = []
27
- end
28
-
29
- def rule(what_to_capture, &block)
30
- rule = Rule.new(what_to_capture, &block)
31
- @rules << rule
32
- rule
33
- end
34
-
35
- def find_rules_that_match_against(query)
36
- @rules.find_all { |rule| !query.to_s.match(rule.what_to_capture).nil? }
37
- end
38
- end
39
-
40
- class RuleBook
41
- module IncludeMethods
42
- def method_missing(meth, *args, &block)
43
- rulebook = self.class.const_get('INSTANCE_RULEBOOK')
44
- rules = rulebook.find_rules_that_match_against(meth)
45
-
46
- unless rules.nil? || rules.empty?
47
- rule = rules.first
48
- match = rule.match_against(meth)
49
- instance_exec(*(match.captures||[] + args)[0...rule.block.arity], &rule.block)
50
- else
51
- super
52
- end
53
- end
54
- end
55
-
56
- module ExtendMethods
57
- def method_missing(meth, *args, &block)
58
- rulebook = const_get('CLASS_NOTEBOOK')
59
- rules = rulebook.find_rules_that_match_against(meth)
60
-
61
- unless rules.nil?
62
- rule = rules.first
63
- match = rule.match_against(meth)
64
- class_exec(*(match.captures||[] + args)[0...rule.block.arity], &rule.block)
65
- else
66
- super
67
- end
68
- end
69
- end
70
- end
71
-
72
- class Module
73
- def rule(what_to_capture, &block)
74
- raise(ArgumentError, 'rules must have a block') unless block_given?
75
-
76
- setup_rulebook('INSTANCE_RULEBOOK', :include)
77
- const_get('INSTANCE_RULEBOOK').rule(what_to_capture, &block)
78
- end
79
-
80
- def class_rule(what_to_capture, &block)
81
- raise(ArgumentError, 'class_rules must have a block') unless block_given?
82
-
83
- setup_rulebook('CLASS_NOTEBOOK', :extend)
84
- const_get('CLASS_NOTEBOOK').rule(what_to_capture, &block)
85
- end
86
-
87
- def rules(&block)
88
- raise(ArgumentError, 'rules must have a block') unless block_given?
89
-
90
- setup_rulebook('INSTANCE_RULEBOOK', :include)
91
- const_get('INSTANCE_RULEBOOK').instance_eval(&block)
92
- const_get('INSTANCE_RULEBOOK')
93
- end
94
-
95
- def class_rules(&block)
96
- raise(ArgumentError, 'class_rules must have a block') unless block_given?
97
-
98
- setup_rulebook('CLASS_NOTEBOOK', :extend)
99
- const_get('CLASS_NOTEBOOK').instance_eval(&block)
100
- const_get('CLASS_NOTEBOOK')
101
- end
102
-
103
- private
104
-
105
- def setup_rulebook(rulebook_constant, extend_or_include)
106
- raise(ArgumentError, 'extend_or_include must be :extend or :include') \
107
- unless [:extend, :include].include?(extend_or_include)
108
-
109
- unless const_defined?(rulebook_constant)
110
- const_set(rulebook_constant, RuleBook.new)
111
-
112
- module_name = extend_or_include.to_s.capitalize + 'Methods'
113
- send(extend_or_include, RuleBook.const_get(module_name))
114
- end
115
- end
116
- end
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rulebook}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
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"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ryan Lewis