irregular_method 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +24 -0
- data/LICENSE +20 -0
- data/README.rdoc +71 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/irregular_method.gemspec +59 -0
- data/lib/irregular_method.rb +47 -0
- data/spec/irregular_method_spec.rb +14 -0
- data/spec/models/method_missing.rb +19 -0
- data/spec/schema.rb +8 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +19 -0
- metadata +80 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Mike Nelson
|
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.rdoc
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
= irregular_method
|
2
|
+
|
3
|
+
Writing a bunch of case statements to handle regular expressions in method missing is just plain annoying. I suggest they get separated out and defined as their own methods. Well... ruby doesn't really let that happen, but there is an alternative.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
Install the gem
|
8
|
+
gem install irregular_method
|
9
|
+
|
10
|
+
Add it to your environment.rb
|
11
|
+
|
12
|
+
config.gem 'irregular_method'
|
13
|
+
|
14
|
+
== Usage
|
15
|
+
|
16
|
+
The purpose of irregular_method is to allow methods with regular expression names to be defined. To do so, simply define a method using the reg_def command.
|
17
|
+
|
18
|
+
reg_def( /^total_(.+)$/) do |m, args|
|
19
|
+
...
|
20
|
+
# do something with the match data and arguments
|
21
|
+
...
|
22
|
+
end
|
23
|
+
|
24
|
+
All reg_def methods are passed back the MatchData and the arguments passed to the call. For instance, let's say we have this class:
|
25
|
+
|
26
|
+
class Layer < ActiveRecord::Base
|
27
|
+
|
28
|
+
cattr_accessor :layers
|
29
|
+
attr_accessible :index
|
30
|
+
|
31
|
+
reg_def(/^layer_(.+)$/) do |m, args|
|
32
|
+
above_or_below = m[1].downcase #first match in the expression
|
33
|
+
layers[self.index + (above_or_below == 'above' && 1 || above_or_below == 'below' && -1 || 0)]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
What this is doing is matching a method looking like "layer_XXX". If the match (XXX) is 'above', than it's giving back the layer above the current one. If it's 'below' than it's giving back the layer below.
|
38
|
+
What's nice about this is you no longer need to write a bunch of messy code in your method_missing. Instead, write it as reg_def's and have a cleaner definition of your class. Let's take a look at one more example:
|
39
|
+
|
40
|
+
class Example < ActiveRecord::Base
|
41
|
+
|
42
|
+
has_many :users
|
43
|
+
has_many :cars
|
44
|
+
|
45
|
+
reg_def [/^capable_(.+)s$/, /^capable_(.+)$/] do |match, args|
|
46
|
+
self.send["#{m[1]}s"].select{|item| self.capable?(item)}
|
47
|
+
end
|
48
|
+
|
49
|
+
def capable?(something)
|
50
|
+
... # do something to determine if this object is "capable"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
e = Example.find 1
|
55
|
+
e.users
|
56
|
+
=> [#<User id: 1, created_at: "2010-01-27 01:09:42", updated_at: "2010-01-27 01:09:42">, #<User id: 4, created_at: "2010-01-27 03:09:42", updated_at: "2010-01-27 03:09:42">]
|
57
|
+
|
58
|
+
e.capable_users
|
59
|
+
=> [#<User id: 1, created_at: "2010-01-27 01:09:42", updated_at: "2010-01-27 01:09:42">]
|
60
|
+
|
61
|
+
You may have noticed that you can pass an array to to the reg_def. It will check each element of that array for a match. ORDER IS IMPORTANT! Take the example case... If we swapped the order of the regular expressions we would be sending self the method "userss" which would obviously be incorrect. Now, please don't yell at me... I understand I should be using m[1].pluralize.... It's an EXAMPLE... and I didn't want to think too hard. It hurts when I do that.
|
62
|
+
|
63
|
+
---
|
64
|
+
|
65
|
+
== Important Things (not from Demetri Martin)
|
66
|
+
|
67
|
+
The reference to 'self' IS a reference to the instance of the class. Normally, it would be a reference to the class since it's being evaluated as a class method, but there's a workaround. In any case, using self to reference the instance of the class you're working with is absolutely correct.
|
68
|
+
|
69
|
+
== Copyright
|
70
|
+
|
71
|
+
Copyright (c) 2010 Mike Nelson. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "irregular_method"
|
8
|
+
gem.summary = "Method missing is messy. Fix it by defining regular expression methods."
|
9
|
+
gem.description = "Writing a bunch of case statements to handle regular expressions in method missing is just plain annoying. I suggest they get separated out and defined as their own methods. Well... ruby doesn't really let that happen, but there is an alternative."
|
10
|
+
gem.email = "mdnelson30@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/mnelson/irregular_method"
|
12
|
+
gem.authors = ["Mike Nelson"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
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 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "irregular_method #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,59 @@
|
|
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{irregular_method}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Mike Nelson"]
|
12
|
+
s.date = %q{2010-02-01}
|
13
|
+
s.description = %q{Writing a bunch of case statements to handle regular expressions in method missing is just plain annoying. I suggest they get separated out and defined as their own methods. Well... ruby doesn't really let that happen, but there is an alternative.}
|
14
|
+
s.email = %q{mdnelson30@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"irregular_method.gemspec",
|
27
|
+
"lib/irregular_method.rb",
|
28
|
+
"spec/irregular_method_spec.rb",
|
29
|
+
"spec/models/method_missing.rb",
|
30
|
+
"spec/schema.rb",
|
31
|
+
"spec/spec.opts",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/mnelson/irregular_method}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.5}
|
38
|
+
s.summary = %q{Method missing is messy. Fix it by defining regular expression methods.}
|
39
|
+
s.test_files = [
|
40
|
+
"spec/irregular_method_spec.rb",
|
41
|
+
"spec/models/method_missing.rb",
|
42
|
+
"spec/schema.rb",
|
43
|
+
"spec/spec_helper.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Irregular
|
2
|
+
module Method
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend Irregular::Method::ClassMethods
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def reg_def(regex, &block)
|
12
|
+
if(!self.included_modules.include?(Irregular::Method::InstanceMethods))
|
13
|
+
send(:include, Irregular::Method::InstanceMethods)
|
14
|
+
send('irregular_methods=', [])
|
15
|
+
end
|
16
|
+
irregular_methods << [[regex].flatten.compact, block]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module InstanceMethods
|
21
|
+
def self.included(base)
|
22
|
+
base.class_eval do
|
23
|
+
cattr_accessor :irregular_methods
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def method_missing(name, *args)
|
29
|
+
method_id = name.id2name
|
30
|
+
irregular_methods && irregular_methods.each do |context|
|
31
|
+
regex = context[0]
|
32
|
+
block = context[1]
|
33
|
+
regex.each do |reg|
|
34
|
+
if method_id =~ reg
|
35
|
+
return block.bind(self).call($~, args)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
super
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
::ActiveRecord::Base.send :include, ::Irregular::Method
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "IrregularMethod" do
|
4
|
+
it "should implement and bind reg_def properly" do
|
5
|
+
mm = MethodMissing.create(:name => 'mm1')
|
6
|
+
MethodMissing.irregular_methods.size.should eql(4)
|
7
|
+
|
8
|
+
mm.nothing_test.should eql('mm1 found: nothing')
|
9
|
+
mm.dogs_method_cats.should eql('mm1 found 2 things: dogs and cats')
|
10
|
+
mm.argument_tester('hello').should eql('mm1 sees an argument: hello')
|
11
|
+
mm.test_multiple.should eql('mm1 handles multiple options: test')
|
12
|
+
mm.success_options.should eql('mm1 handles multiple options: success')
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class MethodMissing < ActiveRecord::Base
|
2
|
+
|
3
|
+
reg_def(/^(.+)_test$/) do |m, args|
|
4
|
+
"#{self.name} found: #{m[1]}"
|
5
|
+
end
|
6
|
+
|
7
|
+
reg_def(/^(.+)_method_(.+)$/) do |m, args|
|
8
|
+
"#{self.name} found 2 things: #{m[1]} and #{m[2]}"
|
9
|
+
end
|
10
|
+
|
11
|
+
reg_def(/^(.+)_tester$/) do |m, args|
|
12
|
+
"#{self.name} sees an argument: #{args[0]}"
|
13
|
+
end
|
14
|
+
|
15
|
+
reg_def [/^(.+)_multiple$/, /^(.+)_options$/] do |m, args|
|
16
|
+
"#{self.name} handles multiple options: #{m[1]}"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/spec/schema.rb
ADDED
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_record'
|
5
|
+
require 'irregular_method'
|
6
|
+
require 'spec'
|
7
|
+
require 'spec/autorun'
|
8
|
+
require 'models/method_missing'
|
9
|
+
|
10
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
|
11
|
+
ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
12
|
+
ActiveRecord::Base.establish_connection(ENV['DB'] || 'mysql')
|
13
|
+
|
14
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
15
|
+
|
16
|
+
|
17
|
+
Spec::Runner.configure do |config|
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: irregular_method
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Nelson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-01 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
description: Writing a bunch of case statements to handle regular expressions in method missing is just plain annoying. I suggest they get separated out and defined as their own methods. Well... ruby doesn't really let that happen, but there is an alternative.
|
26
|
+
email: mdnelson30@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- irregular_method.gemspec
|
42
|
+
- lib/irregular_method.rb
|
43
|
+
- spec/irregular_method_spec.rb
|
44
|
+
- spec/models/method_missing.rb
|
45
|
+
- spec/schema.rb
|
46
|
+
- spec/spec.opts
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/mnelson/irregular_method
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.3.5
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Method missing is messy. Fix it by defining regular expression methods.
|
76
|
+
test_files:
|
77
|
+
- spec/irregular_method_spec.rb
|
78
|
+
- spec/models/method_missing.rb
|
79
|
+
- spec/schema.rb
|
80
|
+
- spec/spec_helper.rb
|