rulebook 0.0.2 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.md
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ryan Lewis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # rulebook
2
+
3
+ Allows you to define a set of 'rules' or dynamic methods to apply to a class
4
+
5
+ ## Install
6
+
7
+ > gem update --system
8
+ > gem install rulebook
9
+
10
+ ## Example
11
+
12
+ require 'rulebook'
13
+
14
+ class User
15
+ follows_rules
16
+ attr :name, :title
17
+
18
+ def initialize(name)
19
+ @name = name
20
+ @title = :user
21
+ end
22
+
23
+ rule /is_(admin|moderator|super_user|user)/ do |title|
24
+ @title = title.to_sym
25
+ end
26
+ end
27
+
28
+ You can now do things like
29
+
30
+ users = [
31
+ User.new('Ryan'),
32
+ User.new('Natale'),
33
+ User.new('Joe'),
34
+ User.new('Monica'),
35
+ User.new('Matt'),
36
+ User.new('Jess')
37
+ ].shuffle
38
+
39
+ users[0].is_admin
40
+ users[1].is_moderator
41
+ users[2].is_super_user
42
+
43
+ users.each do |user|
44
+ puts "#{user.name} is a #{user.title}"
45
+ end
46
+
47
+ There are more examples in the example directory and easy to understand tests in the tests directory
48
+
49
+ ## Note on Patches/Pull Requests
50
+
51
+ * Fork the project.
52
+ * Make your feature addition or bug fix.
53
+ * Add tests for it. This is important so I don't break it in a
54
+ future version unintentionally.
55
+ * Commit, do not mess with rakefile, version, or history.
56
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
57
+ * Send me a pull request. Bonus points for topic branches.
58
+
59
+ ## Copyright
60
+
61
+ Copyright (c) 2010 Ryan Lewis. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rulebook"
8
+ gem.summary = %Q{Allows you to define a set of 'rules' or dynamic methods to apply to a class}
9
+ gem.description = %Q{Lets you define methods with regex for dynamic methods}
10
+ gem.email = "c00lryguy@gmail.com"
11
+ gem.homepage = "http://github.com/c00lryguy/rulebook"
12
+ gem.authors = ["Ryan Lewis"]
13
+ # gem.add_development_dependency "riot", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = false
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = false
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "rulebook #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
54
+
55
+
56
+ begin
57
+ require 'reek/rake/task'
58
+
59
+ Reek::Rake::Task.new do |t|
60
+ t.fail_on_error = true
61
+ t.verbose = true
62
+ end
63
+ rescue LoadError
64
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/lib/rulebook.rb CHANGED
@@ -1,60 +1,73 @@
1
- module RuleBook
2
- module ClassMethods
3
- # make the 'rule' method private so it cannot be accessed
4
- # outside of the class the follows_rules method was called in
5
- private
6
- def rule(match, &block)
7
- raise(ArgumentError, "block not given") unless block_given?
8
- raise(TypeError, "match must be of type Regexp") unless match.class == Regexp
9
-
10
- Rule.new(self, match, block)
11
- end
1
+ class RuleBook
2
+ class Rule
3
+ attr :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
12
17
  end
18
+ end
19
+
20
+ class RuleBook
21
+ attr :rules
13
22
 
14
- module InstanceMethods
15
- def method_missing(meth, *args)
16
- matched_rules = []
17
- Rule.list_for(self.class).each do |rule|
18
- match = meth.to_s.match(rule.match)
19
- if match.nil?
20
- next
21
- else
22
- matched_rules << rule
23
- end
24
- end
25
-
26
- if matched_rules.empty?
27
- super
28
- else
29
- matched_rules.each do |rule|
30
- blk = rule.block
31
- match = meth.to_s.match(rule.match)
32
- instance_exec(match, &blk)
33
- end
34
- end
35
- end
23
+ def initialize
24
+ @rules = []
36
25
  end
37
26
 
38
- class Rule
39
- class << self
40
- # Rule.list looks better than Rule.rules
41
- def list; @rules ||= {}; end
42
- # Rule.for() looks better than Rule.list[]
43
- def list_for(parent); list[parent]; end
27
+ def add_rule(what_to_capture, &block)
28
+ @rules << Rule.new(what_to_capture, &block)
29
+ end
30
+
31
+ def find_rules_that_match_against(query)
32
+ @rules.find_all { |rule| !query.to_s.match(rule.what_to_capture).nil? }
33
+ end
34
+ end
35
+
36
+ class RuleBook
37
+ # Provides the ClassMethods and InstanceMethods modules that get mixed into
38
+ # the class that #follows_rules is called in
39
+ module Mixin
40
+ module ClassMethods
41
+ def rule(what_to_capture, &block)
42
+ rulebook = const_get('RULEBOOK')
43
+ rulebook.add_rule(what_to_capture, &block)
44
+ end
44
45
  end
45
46
 
46
- attr :match, :block
47
- def initialize(parent, match, block)
48
- @match, @block = match, block
49
- Rule.list[parent] ||= []
50
- Rule.list[parent] << self
47
+ module InstanceMethods
48
+ def method_missing(meth, *args, &blk)
49
+ rulebook = self.class.const_get('RULEBOOK')
50
+ rules = rulebook.find_rules_that_match_against(meth)
51
+
52
+ unless rules.nil?
53
+ rules.first.tap do |rule|
54
+ match = rule.match_against(meth)
55
+ instance_exec(*match.captures, *args, &rule.block)
56
+ end
57
+ else
58
+ super
59
+ end
60
+ end
51
61
  end
52
62
  end
53
63
  end
54
64
 
55
65
  class Module
66
+ # Mixes in RuleBook::Mixin::ClassMethods and RuleBook::Mixin::InstanceMethods
67
+ # TODO: allow argument to use other RuleBook instances.. also multiple rulebooks
56
68
  def follows_rules
57
- extend RuleBook::ClassMethods
58
- include RuleBook::InstanceMethods
69
+ const_set('RULEBOOK', RuleBook.new)
70
+ extend RuleBook::Mixin::ClassMethods
71
+ include RuleBook::Mixin::InstanceMethods
59
72
  end
60
73
  end
data/rulebook.gemspec ADDED
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rulebook}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Lewis"]
12
+ s.date = %q{2010-04-25}
13
+ s.description = %q{Lets you define methods with regex for dynamic methods}
14
+ s.email = %q{c00lryguy@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rulebook.rb",
27
+ "rulebook.gemspec",
28
+ "test/helper.rb",
29
+ "test/test_chevy.rb",
30
+ "test/test_ryguy.rb",
31
+ "test/test_user.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/c00lryguy/rulebook}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.6}
37
+ s.summary = %q{Allows you to define a set of 'rules' or dynamic methods to apply to a class}
38
+ s.test_files = [
39
+ "test/helper.rb",
40
+ "test/test_chevy.rb",
41
+ "test/test_ryguy.rb",
42
+ "test/test_user.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
55
+
data/test/helper.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'shoulda'
3
+ require 'test/unit'
4
+
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
11
+
12
+ end
@@ -0,0 +1,66 @@
1
+ require 'helper'
2
+
3
+ class TestChevy < Test::Unit::TestCase
4
+ class Engine
5
+ follows_rules
6
+ attr :state
7
+
8
+ def initialize
9
+ @state = "off"
10
+ end
11
+
12
+ rule(/is_(.*)/) do |state|
13
+ @state = state.gsub(/_/, " ")
14
+ end
15
+ end
16
+
17
+ context 'A Chevy engine checked with #state_is?' do
18
+ setup do
19
+ @chevy = Engine.new
20
+ class << @chevy
21
+ def state_is?(state)
22
+ @state == state
23
+ end
24
+ end
25
+ end
26
+
27
+ should 'be off' do
28
+ assert @chevy.state_is?('off')
29
+ end
30
+
31
+ should 'be idling' do
32
+ @chevy.is_idling
33
+ assert @chevy.state_is?('idling')
34
+ end
35
+
36
+ should 'be broken as usual' do
37
+ @chevy.is_broken_as_usual
38
+ assert @chevy.state_is?('broken as usual')
39
+ end
40
+ end
41
+
42
+ context 'A Chevy engine checked with custom rule' do
43
+ setup do
44
+ @chevy = Engine.new
45
+ class << @chevy
46
+ rule(/is_(.*)?/) do |state|
47
+ @state == state
48
+ end
49
+ end
50
+ end
51
+
52
+ should 'be off' do
53
+ assert @chevy.is_off?
54
+ end
55
+
56
+ should 'be idling' do
57
+ @chevy.is_idling
58
+ assert @chevy.is_idling?
59
+ end
60
+
61
+ should 'be broken as usual' do
62
+ @chevy.is_broken_as_usual
63
+ assert @chevy.is_broken_as_usual?
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,44 @@
1
+ require 'helper'
2
+
3
+ class TestRyguy < Test::Unit::TestCase
4
+ class Ryguy
5
+ follows_rules
6
+ attr :nouns, :adjectives
7
+
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
+
19
+ context 'Ryguy' do
20
+ setup do
21
+ @ryguy = Ryguy.new
22
+ @ryguy.is_awesome
23
+ @ryguy.is_a_bear
24
+ @ryguy.is_superfly
25
+ @ryguy.is_a_programmer
26
+ @ryguy.is_fantastic
27
+ @ryguy.is_a_master_of_the_ancient_chinese_art_of_karate
28
+ end
29
+
30
+ should 'be awesome, superfly, and fantastic' do
31
+ assert_same_elements(
32
+ ['awesome', 'superfly', 'fantastic'],
33
+ @ryguy.adjectives
34
+ )
35
+ end
36
+
37
+ should 'be a bear, a programmer, and a master of karate' do
38
+ assert_same_elements(
39
+ ['bear', 'programmer', 'master of the ancient chinese art of karate'],
40
+ @ryguy.nouns
41
+ )
42
+ end
43
+ end
44
+ end
data/test/test_user.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'helper'
2
+ require 'date'
3
+
4
+ class TestUser < Test::Unit::TestCase
5
+ class User
6
+ follows_rules
7
+ attr :gender, :height, :eye_color, :date_of_birth, :age
8
+
9
+ rule(/is_(male|female)/) do |gender|
10
+ @gender = gender.to_sym
11
+ end
12
+
13
+ rule(/is_(\d+)_foot_(\d+)(_inches)?/) do |feet, inches|
14
+ @height = "#{feet}-#{inches}"
15
+ end
16
+
17
+ rule(/has_(amber|blue|brown|gray|grey|green|hazel|red)_eyes/) do |eye_color|
18
+ @eye_color = eye_color.to_sym
19
+ end
20
+
21
+ rule(/was_born_([a-z]+_\d+(st|nd|rd|th)_\d+)/) do |date_of_birth|
22
+ @date_of_birth = Date.parse(date_of_birth.gsub(/_/, ' '))
23
+ @age = (Date.today - @date_of_birth).to_i / 365
24
+ end
25
+ end
26
+
27
+ context 'A User instance' do
28
+ setup do
29
+ @ryguy = User.new
30
+ @ryguy.is_male
31
+ @ryguy.is_5_foot_8
32
+ @ryguy.has_brown_eyes
33
+ @ryguy.was_born_january_15th_1991
34
+ end
35
+
36
+ should 'be valid' do
37
+ assert_equal :male, @ryguy.gender
38
+ assert_equal '5-8', @ryguy.height
39
+ assert_equal :brown, @ryguy.eye_color
40
+ assert_equal Date.parse('January 15th, 1991'), @ryguy.date_of_birth
41
+ assert_equal 19, @ryguy.age
42
+ end
43
+ end
44
+ end
metadata CHANGED
@@ -4,38 +4,49 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 0
8
- - 2
9
- version: 0.0.2
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
- - c00lryguy
12
+ - Ryan Lewis
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-02 00:00:00 -04:00
17
+ date: 2010-04-25 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: Apply rules to classes for defining dynamic methods
22
- email:
23
- - c00lryguy+rulebook@gmail.com
21
+ description: Lets you define methods with regex for dynamic methods
22
+ email: c00lryguy@gmail.com
24
23
  executables: []
25
24
 
26
25
  extensions: []
27
26
 
28
- extra_rdoc_files: []
29
-
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.md
30
30
  files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.md
35
+ - Rakefile
36
+ - VERSION
31
37
  - lib/rulebook.rb
38
+ - rulebook.gemspec
39
+ - test/helper.rb
40
+ - test/test_chevy.rb
41
+ - test/test_ryguy.rb
42
+ - test/test_user.rb
32
43
  has_rdoc: true
33
- homepage:
44
+ homepage: http://github.com/c00lryguy/rulebook
34
45
  licenses: []
35
46
 
36
47
  post_install_message:
37
- rdoc_options: []
38
-
48
+ rdoc_options:
49
+ - --charset=UTF-8
39
50
  require_paths:
40
51
  - lib
41
52
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -58,6 +69,9 @@ rubyforge_project:
58
69
  rubygems_version: 1.3.6
59
70
  signing_key:
60
71
  specification_version: 3
61
- summary: Make your classes follow the rules
62
- test_files: []
63
-
72
+ summary: Allows you to define a set of 'rules' or dynamic methods to apply to a class
73
+ test_files:
74
+ - test/helper.rb
75
+ - test/test_chevy.rb
76
+ - test/test_ryguy.rb
77
+ - test/test_user.rb