rulebook 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ class Rulebook
2
+ module ClassMethods
3
+ # Finds or creates an instrance of Rulebook
4
+ def rulebook; @rulebook ||= Rulebook.new; end
5
+ end
6
+ end
@@ -0,0 +1,24 @@
1
+ require 'meta_tools'
2
+
3
+ #++
4
+ # Should this be class Object?
5
+ # Is there really a difference?
6
+ #--
7
+ class Module
8
+ # This declares that the current class has a rulebook
9
+ # with rules that it wants it's instances to follow.
10
+ #
11
+ # I picked this name because the probability of someone needing the method
12
+ # 'follows_the_rules!' (with a bang) is slim... hopefully.
13
+ #
14
+ # This adds the following method:
15
+ # User#rulebook
16
+ # Returns the RuleBook instance that contains the defined rules.
17
+ def follows_the_rules!
18
+ extend(MetaTools)
19
+ include(MetaTools)
20
+
21
+ extend(Rulebook::ClassMethods)
22
+ include(Rulebook::InstanceMethods)
23
+ end
24
+ end
@@ -0,0 +1,41 @@
1
+ class Rulebook
2
+ module InstanceMethods
3
+ def method_missing(meth, *args, &blk)
4
+ # Classes and instances find their rulebook differently.
5
+ begin
6
+ rulebook = self.class.rulebook
7
+ rescue NoMethodError
8
+ rulebook = metaclass.rulebook
9
+ end
10
+
11
+ rules = rulebook.rules_that_match_against(meth)
12
+ unless rules.nil?
13
+ rules.each do |rule|
14
+ # =S Run all matched rules or run first matched rule...?
15
+ # rule = rules.first
16
+
17
+ captures = rule[meth].captures || []
18
+ block = rule.block
19
+
20
+ # Remove the possibility of optional arguments
21
+ arity = block.arity == -1 ? 0 : block.arity
22
+
23
+ # Define the method
24
+ meta_def(meth) do |*args|
25
+ instance_exec(*(captures + args).take(arity), &block)
26
+ end
27
+
28
+ # klass = self.class
29
+ # klass.send(:define_method, meth) do |*args|
30
+ # instance_exec(*(captures + args).take(arity), &block)
31
+ # end
32
+
33
+ # Call the method and return the result
34
+ return send(meth, *args, &block)
35
+ end
36
+ else
37
+ super
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,18 @@
1
+ class Rulebook
2
+ # This class creates an instance of a Rule, which holds the
3
+ # Regexp to match against and the block to run when matched
4
+ class Rule
5
+ attr :block
6
+
7
+ def initialize(what_to_capture, &block)
8
+ # TODO: Match more than Regexp. Strings and Symbols pls.
9
+ raise(TypeError, 'what_to_capture must be of type Regexp') unless what_to_capture.is_a?(Regexp)
10
+ raise(ArgumentError, 'a block is needed') unless block_given?
11
+ @what_to_capture, @block = what_to_capture, block
12
+ end
13
+
14
+ def [](query); query.to_s.downcase.match(@what_to_capture); end
15
+ alias_method :match_against, :[]
16
+ def matches_against?(query); !self[query].nil?; end
17
+ end
18
+ end
data/rulebook.gemspec CHANGED
@@ -1,61 +1,81 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rulebook}
8
- s.version = "0.3.3"
8
+ s.version = "0.4.0"
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-10}
13
- s.description = %q{Lets you define methods with regex for dynamic methods}
12
+ s.date = %q{2011-04-30}
13
+ s.description = %q{Allows you to define a set of 'rules' or dynamic methods to apply to a class.}
14
14
  s.email = %q{c00lryguy@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.md",
18
- "TODO"
17
+ "README.md"
19
18
  ]
20
19
  s.files = [
21
20
  ".document",
22
- ".gitignore",
23
- "LICENSE",
24
- "README.md",
25
- "Rakefile",
26
- "TODO",
27
- "VERSION",
28
- "lib/rulebook.rb",
29
- "rulebook.gemspec",
30
- "test/helper.rb",
31
- "test/test_chevy.rb",
32
- "test/test_class_methods.rb",
33
- "test/test_rules.rb",
34
- "test/test_ryguy.rb",
35
- "test/test_user.rb"
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "examples/simple.rb",
28
+ "lib/rulebook.rb",
29
+ "lib/rulebook/class_methods.rb",
30
+ "lib/rulebook/core_ext/module.rb",
31
+ "lib/rulebook/instance_methods.rb",
32
+ "lib/rulebook/rule.rb",
33
+ "rulebook.gemspec",
34
+ "test/helper.rb",
35
+ "test/test_chevy.rb",
36
+ "test/test_rule.rb"
36
37
  ]
37
38
  s.homepage = %q{http://github.com/c00lryguy/rulebook}
38
- s.rdoc_options = ["--charset=UTF-8"]
39
+ s.licenses = ["MIT"]
39
40
  s.require_paths = ["lib"]
40
- s.rubygems_version = %q{1.3.6}
41
- s.summary = %q{Allows you to define a set of 'rules' or dynamic methods to apply to a class}
41
+ s.rubygems_version = %q{1.3.7}
42
+ s.summary = %q{Define methods with regex for dynamic methods.}
42
43
  s.test_files = [
44
+ "examples/simple.rb",
43
45
  "test/helper.rb",
44
- "test/test_chevy.rb",
45
- "test/test_class_methods.rb",
46
- "test/test_rules.rb",
47
- "test/test_ryguy.rb",
48
- "test/test_user.rb"
46
+ "test/test_chevy.rb",
47
+ "test/test_rule.rb"
49
48
  ]
50
49
 
51
50
  if s.respond_to? :specification_version then
52
51
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
52
  s.specification_version = 3
54
53
 
55
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
+ s.add_runtime_dependency(%q<meta_tools>, [">= 0"])
56
+ s.add_development_dependency(%q<rake>, [">= 0"])
57
+ s.add_development_dependency(%q<riot>, [">= 0"])
58
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
59
+ s.add_runtime_dependency(%q<meta_tools>, ["> 0.1"])
60
+ s.add_development_dependency(%q<rake>, ["> 0.0.0"])
61
+ s.add_development_dependency(%q<riot>, ["> 0.0.0"])
56
62
  else
63
+ s.add_dependency(%q<meta_tools>, [">= 0"])
64
+ s.add_dependency(%q<rake>, [">= 0"])
65
+ s.add_dependency(%q<riot>, [">= 0"])
66
+ s.add_dependency(%q<jeweler>, [">= 0"])
67
+ s.add_dependency(%q<meta_tools>, ["> 0.1"])
68
+ s.add_dependency(%q<rake>, ["> 0.0.0"])
69
+ s.add_dependency(%q<riot>, ["> 0.0.0"])
57
70
  end
58
71
  else
72
+ s.add_dependency(%q<meta_tools>, [">= 0"])
73
+ s.add_dependency(%q<rake>, [">= 0"])
74
+ s.add_dependency(%q<riot>, [">= 0"])
75
+ s.add_dependency(%q<jeweler>, [">= 0"])
76
+ s.add_dependency(%q<meta_tools>, ["> 0.1"])
77
+ s.add_dependency(%q<rake>, ["> 0.0.0"])
78
+ s.add_dependency(%q<riot>, ["> 0.0.0"])
59
79
  end
60
80
  end
61
81
 
data/test/helper.rb CHANGED
@@ -1,12 +1,14 @@
1
1
  require 'rubygems'
2
- require 'shoulda'
3
- require 'test/unit'
4
2
 
5
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
- $LOAD_PATH.unshift(File.dirname(__FILE__))
7
-
8
- require 'rulebook'
9
-
10
- class Test::Unit::TestCase
3
+ require 'riot'
11
4
 
5
+ # Stupid non-colored Windows terminal
6
+ class Riot::IOReporter
7
+ def red(msg); msg; end
8
+ def green(msg); msg; end
9
+ def yellow(msg); msg; end
12
10
  end
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'rulebook'
data/test/test_chevy.rb CHANGED
@@ -1,69 +1,56 @@
1
- require 'helper'
2
-
3
- class TestChevy < Test::Unit::TestCase
4
- class Engine
5
- attr_accessor :state
6
-
7
- def initialize
8
- @state = "off"
9
- end
10
-
11
- rules do
12
- rule(/is_(.*)/) do |state|
13
- @state = state.gsub(/_/, " ")
14
- end
15
- end
16
- end
17
-
18
- context 'A Chevy engine checked with #state_is?' do
19
- setup do
20
- @chevy = Engine.new
21
- class << @chevy
22
- def state_is?(state)
23
- @state == state
24
- end
25
- end
26
- end
27
-
28
- should 'be off' do
29
- assert @chevy.state_is?('off')
30
- end
31
-
32
- should 'be idling' do
33
- @chevy.is_idling
34
- assert @chevy.state_is?('idling')
35
- end
36
-
37
- should 'be broken as usual' do
38
- @chevy.is_broken_as_usual
39
- assert @chevy.state_is?('broken as usual')
40
- end
41
- end
42
-
43
- context 'A Chevy engine checked with custom rule' do
44
- setup do
45
- @chevy = Engine.new
46
- class << @chevy
47
- rules do
48
- rule(/is_(.*)?/) do |state|
49
- @state == state
50
- end
51
- end
52
- end
53
- end
54
-
55
- should 'be off' do
56
- assert @chevy.is_off?
57
- end
58
-
59
- should 'be idling' do
60
- @chevy.is_idling
61
- assert @chevy.is_idling?
62
- end
63
-
64
- should 'be broken as usual' do
65
- @chevy.is_broken_as_usual
66
- assert @chevy.is_broken_as_usual?
67
- end
68
- end
69
- end
1
+ require 'helper'
2
+
3
+ class Engine
4
+ follows_the_rules!
5
+
6
+ attr_accessor :state
7
+
8
+ def initialize
9
+ @state = :off
10
+ end
11
+
12
+ rulebook.add(/is_(off|idle|broken)/) do |state|
13
+ @state = state.to_sym
14
+ self
15
+ end
16
+
17
+ rulebook.add(/is_(off|idle|broken)\?/) do |state|
18
+ @state == state.to_sym
19
+ end
20
+ end
21
+
22
+ context "A Chevy engine" do
23
+ setup { Engine.new }
24
+
25
+ context "that is off" do
26
+ asserts_topic.assigns(:state, :off)
27
+ asserts(:state).equals(:off)
28
+ end
29
+
30
+ context "that is idle" do
31
+ setup { topic.is_idle }
32
+ asserts_topic.assigns(:state, :idle)
33
+ asserts(:state).equals(:idle)
34
+ end
35
+
36
+ context "that is broken as usual" do
37
+ setup { topic.is_broken }
38
+ asserts_topic.assigns(:state, :broken)
39
+ end
40
+
41
+ context "checked with custom rule" do
42
+ context "that is off" do
43
+ asserts(:is_off?)
44
+ end
45
+
46
+ context "that is idle" do
47
+ setup { topic.is_idle }
48
+ asserts(:is_idle?)
49
+ end
50
+
51
+ context "that is broken as usual" do
52
+ setup { topic.is_broken }
53
+ asserts(:is_broken?)
54
+ end
55
+ end
56
+ end
data/test/test_rule.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'helper'
2
+
3
+ context "A rule all on it's lonesome" do
4
+ setup do
5
+ Rulebook::Rule.new(/is_lonely/) { "='(" }
6
+ end
7
+ asserts("that this rule truly is alone") { topic.matches_against?(:is_lonely) }
8
+ denies("an awkward method") { topic.matches_against?(:is_surrounded_by_other_rule_friends) }
9
+ asserts("this works with Strings") { topic.matches_against?("is_lonely") }
10
+ end
metadata CHANGED
@@ -1,83 +1,176 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rulebook
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ segments:
6
+ - 0
7
+ - 4
6
8
  - 0
7
- - 3
8
- - 3
9
- version: 0.3.3
9
+ prerelease: false
10
10
  platform: ruby
11
- authors:
11
+ authors:
12
12
  - Ryan Lewis
13
- autorequire:
13
+ autorequire: !!null
14
14
  bindir: bin
15
15
  cert_chain: []
16
-
17
- date: 2010-05-10 00:00:00 -04:00
18
- default_executable:
19
- dependencies: []
20
-
21
- description: Lets you define methods with regex for dynamic methods
16
+ date: 2011-04-30 00:00:00.000000000 -04:00
17
+ default_executable: !!null
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: meta_tools
21
+ requirement: &31472964 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ segments:
28
+ - 0
29
+ type: :runtime
30
+ prerelease: false
31
+ version_requirements: *31472964
32
+ - !ruby/object:Gem::Dependency
33
+ name: rake
34
+ requirement: &31472496 !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ segments:
41
+ - 0
42
+ type: :development
43
+ prerelease: false
44
+ version_requirements: *31472496
45
+ - !ruby/object:Gem::Dependency
46
+ name: riot
47
+ requirement: &31472040 !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ segments:
54
+ - 0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *31472040
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &31471572 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *31471572
71
+ - !ruby/object:Gem::Dependency
72
+ name: meta_tools
73
+ requirement: &31471092 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>'
77
+ - !ruby/object:Gem::Version
78
+ version: '0.1'
79
+ segments:
80
+ - 0
81
+ - 1
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: *31471092
85
+ - !ruby/object:Gem::Dependency
86
+ name: rake
87
+ requirement: &31470552 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>'
91
+ - !ruby/object:Gem::Version
92
+ version: 0.0.0
93
+ segments:
94
+ - 0
95
+ - 0
96
+ - 0
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: *31470552
100
+ - !ruby/object:Gem::Dependency
101
+ name: riot
102
+ requirement: &31470000 !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>'
106
+ - !ruby/object:Gem::Version
107
+ version: 0.0.0
108
+ segments:
109
+ - 0
110
+ - 0
111
+ - 0
112
+ type: :development
113
+ prerelease: false
114
+ version_requirements: *31470000
115
+ description: Allows you to define a set of 'rules' or dynamic methods to apply to
116
+ a class.
22
117
  email: c00lryguy@gmail.com
23
118
  executables: []
24
-
25
119
  extensions: []
26
-
27
- extra_rdoc_files:
120
+ extra_rdoc_files:
28
121
  - LICENSE
29
122
  - README.md
30
- - TODO
31
- files:
123
+ files:
32
124
  - .document
33
- - .gitignore
125
+ - Gemfile
126
+ - Gemfile.lock
34
127
  - LICENSE
35
128
  - README.md
36
129
  - Rakefile
37
- - TODO
38
130
  - VERSION
131
+ - examples/simple.rb
39
132
  - lib/rulebook.rb
133
+ - lib/rulebook/class_methods.rb
134
+ - lib/rulebook/core_ext/module.rb
135
+ - lib/rulebook/instance_methods.rb
136
+ - lib/rulebook/rule.rb
40
137
  - rulebook.gemspec
41
138
  - test/helper.rb
42
139
  - test/test_chevy.rb
43
- - test/test_class_methods.rb
44
- - test/test_rules.rb
45
- - test/test_ryguy.rb
46
- - test/test_user.rb
140
+ - test/test_rule.rb
47
141
  has_rdoc: true
48
142
  homepage: http://github.com/c00lryguy/rulebook
49
- licenses: []
50
-
51
- post_install_message:
52
- rdoc_options:
53
- - --charset=UTF-8
54
- require_paths:
143
+ licenses:
144
+ - MIT
145
+ post_install_message: !!null
146
+ rdoc_options: []
147
+ require_paths:
55
148
  - lib
56
- required_ruby_version: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- segments:
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ segments:
61
156
  - 0
62
- version: "0"
63
- required_rubygems_version: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- segments:
157
+ hash: 224911955
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ none: false
160
+ requirements:
161
+ - - ! '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ segments:
68
165
  - 0
69
- version: "0"
70
166
  requirements: []
71
-
72
- rubyforge_project:
73
- rubygems_version: 1.3.6
74
- signing_key:
167
+ rubyforge_project: !!null
168
+ rubygems_version: 1.3.7
169
+ signing_key: !!null
75
170
  specification_version: 3
76
- summary: Allows you to define a set of 'rules' or dynamic methods to apply to a class
77
- test_files:
171
+ summary: Define methods with regex for dynamic methods.
172
+ test_files:
173
+ - examples/simple.rb
78
174
  - test/helper.rb
79
175
  - test/test_chevy.rb
80
- - test/test_class_methods.rb
81
- - test/test_rules.rb
82
- - test/test_ryguy.rb
83
- - test/test_user.rb
176
+ - test/test_rule.rb