eager_beaver 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +60 -0
- data/Rakefile +2 -0
- data/eager_beaver.gemspec +21 -0
- data/lib/eager_beaver.rb +43 -0
- data/lib/eager_beaver/method_matcher.rb +28 -0
- data/lib/eager_beaver/version.rb +3 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kevin Burleigh
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# EagerBeaver
|
2
|
+
|
3
|
+
Facilitates method_missing, respond_to_missing?, and method-generation activities
|
4
|
+
by providing a simple interface for adding method generators. All related
|
5
|
+
activities, such as registering with #method_missing and #respond_to_missing?
|
6
|
+
are handled automatically. Facilitates method name pattern-specific method
|
7
|
+
generation as well. Generated methods are added to the missing method receiver.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'eager_beaver'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install eager_beaver
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'eager_beaver'
|
27
|
+
|
28
|
+
class NeedsMethods
|
29
|
+
include EagerBeaver
|
30
|
+
|
31
|
+
add_method_matcher do |mm|
|
32
|
+
mm.matcher = Proc.new do
|
33
|
+
/\Amake_(\w+)\z/ =~ missing_method_name
|
34
|
+
@attr_name = Regexp.last_match ? Regexp.last_match[1] : nil
|
35
|
+
Regexp.last_match
|
36
|
+
end
|
37
|
+
mm.new_method_code_maker = Proc.new do
|
38
|
+
%Q{
|
39
|
+
def #{missing_method_name}(arg)
|
40
|
+
puts "#{@attr_name} \#{arg}"
|
41
|
+
end
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
nm = NeedsMethods.new
|
48
|
+
|
49
|
+
nm.make_thingy(10) # thingy 10
|
50
|
+
nm.make_widget("hi") # widget hi
|
51
|
+
nm.oh_no! # (NoMethodError)
|
52
|
+
```
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/eager_beaver/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Kevin Burleigh"]
|
6
|
+
gem.email = ["klb@kindlinglabs.com"]
|
7
|
+
gem.description = %q{Facilitates method_missing, respond_to_missing?, and method-generation activities}
|
8
|
+
gem.summary = %q{Facilitates method_missing, respond_to_missing?, and method-generation activities
|
9
|
+
by providing a simple interface for adding method generators. All related
|
10
|
+
activities, such as registering with #method_missing and #respond_to_missing?
|
11
|
+
are handled automatically. Facilitates method name pattern-specific method
|
12
|
+
generation as well. Generated methods are added to the missing method receiver.}
|
13
|
+
gem.homepage = "http://github.com/kevinburleigh75/eager_beaver"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($\)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.name = "eager_beaver"
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
gem.version = EagerBeaver::VERSION
|
21
|
+
end
|
data/lib/eager_beaver.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "eager_beaver/version"
|
2
|
+
require "eager_beaver/method_matcher"
|
3
|
+
|
4
|
+
module EagerBeaver
|
5
|
+
|
6
|
+
def self.included(includer)
|
7
|
+
includer.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(method_name, *args, &block)
|
11
|
+
self.class.method_matchers.each do |method_matcher|
|
12
|
+
method_matcher.original_caller = self
|
13
|
+
if method_matcher.match?(method_name)
|
14
|
+
if method_matcher.new_method
|
15
|
+
self.class.send(:define_method, method_name, method_matcher.new_method)
|
16
|
+
elsif method_matcher.new_method_code_maker
|
17
|
+
method_string = method_matcher.instance_eval &method_matcher.new_method_code_maker
|
18
|
+
self.class.class_eval method_string, __FILE__, __LINE__ + 1
|
19
|
+
end
|
20
|
+
return self.send(method_name, *args, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
super
|
24
|
+
end
|
25
|
+
|
26
|
+
def respond_to_missing?(method_name, include_private=false)
|
27
|
+
self.class.method_matchers.each do |method_matcher|
|
28
|
+
return true if method_matcher.match?(method_name)
|
29
|
+
end
|
30
|
+
super
|
31
|
+
end
|
32
|
+
|
33
|
+
module ClassMethods
|
34
|
+
def method_matchers
|
35
|
+
@method_matchers ||= []
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_method_matcher(&block)
|
39
|
+
method_matchers << MethodMatcher.new(block)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module EagerBeaver
|
2
|
+
|
3
|
+
class MethodMatcher
|
4
|
+
|
5
|
+
attr_accessor :original_caller
|
6
|
+
attr_accessor :matcher
|
7
|
+
attr_accessor :new_method
|
8
|
+
attr_accessor :new_method_code_maker
|
9
|
+
attr_accessor :missing_method_name
|
10
|
+
|
11
|
+
def initialize(block)
|
12
|
+
block.call(self)
|
13
|
+
|
14
|
+
raise ArgumentError, "matcher Proc must be given" \
|
15
|
+
if matcher.nil?
|
16
|
+
raise ArgumentError, "exactly one of new_method or new_method_code_maker Proc must be given" \
|
17
|
+
if (new_method && new_method_code_maker) || (new_method.nil? && new_method_code_maker.nil?)
|
18
|
+
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def match?(method_name)
|
23
|
+
self.missing_method_name = method_name.to_s
|
24
|
+
self.instance_eval &matcher
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eager_beaver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kevin Burleigh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-04 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Facilitates method_missing, respond_to_missing?, and method-generation
|
15
|
+
activities
|
16
|
+
email:
|
17
|
+
- klb@kindlinglabs.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- eager_beaver.gemspec
|
28
|
+
- lib/eager_beaver.rb
|
29
|
+
- lib/eager_beaver/method_matcher.rb
|
30
|
+
- lib/eager_beaver/version.rb
|
31
|
+
homepage: http://github.com/kevinburleigh75/eager_beaver
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.24
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: ! 'Facilitates method_missing, respond_to_missing?, and method-generation
|
55
|
+
activities by providing a simple interface for adding method generators. All related
|
56
|
+
activities, such as registering with #method_missing and #respond_to_missing? are
|
57
|
+
handled automatically. Facilitates method name pattern-specific method generation
|
58
|
+
as well. Generated methods are added to the missing method receiver.'
|
59
|
+
test_files: []
|