hanging_methods 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +46 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.txt +20 -0
- data/README.md +42 -0
- data/Rakefile +7 -0
- data/hanging_methods.gemspec +24 -0
- data/lib/hanging_methods/method_call_notifier.rb +12 -0
- data/lib/hanging_methods/version.rb +3 -0
- data/lib/hanging_methods.rb +49 -0
- data/spec/hanging_methods/method_call_notifier_spec.rb +24 -0
- data/spec/hanging_methods_spec.rb +51 -0
- data/spec/spec_helper.rb +6 -0
- metadata +94 -0
data/.gitignore
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
*.gem
|
14
|
+
|
15
|
+
# jeweler generated
|
16
|
+
pkg
|
17
|
+
|
18
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
19
|
+
#
|
20
|
+
# * Create a file at ~/.gitignore
|
21
|
+
# * Include files you want ignored
|
22
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
23
|
+
#
|
24
|
+
# After doing this, these files will be ignored in all your git projects,
|
25
|
+
# saving you from having to 'pollute' every project you touch with them
|
26
|
+
#
|
27
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
28
|
+
#
|
29
|
+
# For MacOS:
|
30
|
+
|
31
|
+
.DS_Store
|
32
|
+
|
33
|
+
# For TextMate
|
34
|
+
*.tmproj
|
35
|
+
tmtags
|
36
|
+
.tmtags
|
37
|
+
|
38
|
+
# For emacs:
|
39
|
+
*~
|
40
|
+
\#*
|
41
|
+
.\#*
|
42
|
+
|
43
|
+
# For vim:
|
44
|
+
*.swp
|
45
|
+
|
46
|
+
.rvmrc
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hanging_methods (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.4)
|
10
|
+
rake (10.1.0)
|
11
|
+
rspec (2.14.1)
|
12
|
+
rspec-core (~> 2.14.0)
|
13
|
+
rspec-expectations (~> 2.14.0)
|
14
|
+
rspec-mocks (~> 2.14.0)
|
15
|
+
rspec-core (2.14.4)
|
16
|
+
rspec-expectations (2.14.0)
|
17
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
18
|
+
rspec-mocks (2.14.2)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
hanging_methods!
|
25
|
+
rake
|
26
|
+
rspec (~> 2.14.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Michael Pearce
|
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,42 @@
|
|
1
|
+
# Hanging Methods - Add a method that you can hang other methods
|
2
|
+
|
3
|
+
This is the gem for building nice looking APIs where you want to delegate method calls to another object
|
4
|
+
at a later time.
|
5
|
+
|
6
|
+
See [Parallizer](https://github.com/michaelgpearce/parallizer) for an example.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
gem install hanging_methods
|
11
|
+
|
12
|
+
## Hanging some methods
|
13
|
+
|
14
|
+
Here's an example.
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require 'rubygems'
|
18
|
+
require 'hanging_methods'
|
19
|
+
|
20
|
+
class Interesting
|
21
|
+
include HangingMethods
|
22
|
+
|
23
|
+
add_hanging_method :add
|
24
|
+
end
|
25
|
+
|
26
|
+
interesting = Interesting.new
|
27
|
+
interesting.add.a_method
|
28
|
+
interesting.add.another_method(1, 2)
|
29
|
+
|
30
|
+
puts interesting.hanging_method_invocations(:add)
|
31
|
+
```
|
32
|
+
|
33
|
+
|
34
|
+
# Credits
|
35
|
+
|
36
|
+
[Hanging Methods](https://github.com/michaelgpearce/hanging_methods) is maintained by [Michael Pearce](https://github.com/michaelgpearce)
|
37
|
+
|
38
|
+
|
39
|
+
# Copyright
|
40
|
+
|
41
|
+
Copyright (c) 2013 Michael Pearce. See LICENSE.txt for further details.
|
42
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
require "hanging_methods/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "hanging_methods"
|
8
|
+
s.version = HangingMethods::VERSION
|
9
|
+
s.authors = ["Michael Pearce"]
|
10
|
+
s.email = ["michaelgpearce@yahoo.com"]
|
11
|
+
s.homepage = "http://github.com/michaelgpearce/hanging_methods"
|
12
|
+
s.summary = %q{Add a method that you can hang other methods}
|
13
|
+
s.description = %q{Add a method that you can hang other methods.}
|
14
|
+
s.license = "MIT"
|
15
|
+
|
16
|
+
s.rubyforge_project = "hanging_methods"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency 'rake'
|
23
|
+
s.add_development_dependency 'rspec', '~> 2.14.0'
|
24
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'hanging_methods/version'
|
2
|
+
require 'hanging_methods/method_call_notifier'
|
3
|
+
|
4
|
+
module HangingMethods
|
5
|
+
module ClassMethods
|
6
|
+
def add_hanging_method(hanging_method_name, options = {})
|
7
|
+
unknown_keys = options.keys - [:after_invocation]
|
8
|
+
raise ArgumentError, "Unknown keys: #{unknown_keys.join(',')}" unless unknown_keys.empty?
|
9
|
+
|
10
|
+
add_hanging_method_options(hanging_method_name, options)
|
11
|
+
|
12
|
+
define_method(hanging_method_name) do
|
13
|
+
::HangingMethods::MethodCallNotifier.new do |*method_name_and_args|
|
14
|
+
add_hanging_method_name_and_args_invocation(hanging_method_name, method_name_and_args)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_hanging_method_options(hanging_method_name, options)
|
20
|
+
hanging_method_options(hanging_method_name).merge!(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def hanging_method_options(hanging_method_name)
|
24
|
+
@hanging_method_options ||= {}
|
25
|
+
@hanging_method_options[hanging_method_name] ||= {}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
def add_hanging_method_name_and_args_invocation(hanging_method_name, method_name_and_args)
|
31
|
+
hanging_method_invocations(hanging_method_name) << method_name_and_args
|
32
|
+
|
33
|
+
if after_invocation = self.class.hanging_method_options(hanging_method_name)[:after_invocation]
|
34
|
+
send(after_invocation, method_name_and_args)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def hanging_method_invocations(hanging_method_name)
|
39
|
+
@hanging_method_invocations ||= {}
|
40
|
+
@hanging_method_invocations[hanging_method_name] ||= []
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.included(base)
|
45
|
+
base.send(:include, InstanceMethods)
|
46
|
+
base.send(:extend, ClassMethods)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HangingMethods::MethodCallNotifier do
|
4
|
+
describe "#method_missing" do
|
5
|
+
before do
|
6
|
+
@notifier = HangingMethods::MethodCallNotifier.new do |*args|
|
7
|
+
@callback_args = args
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
subject do
|
12
|
+
@notifier.call_a_method('with args')
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should call block on method call" do
|
16
|
+
subject
|
17
|
+
expect(@callback_args).to eq([:call_a_method, 'with args'])
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return the notifier" do
|
21
|
+
expect(subject).to eq(@notifier)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HangingMethods do
|
4
|
+
class TestObject
|
5
|
+
include HangingMethods
|
6
|
+
|
7
|
+
add_hanging_method :add
|
8
|
+
add_hanging_method :add_more, after_invocation: :add_more_after_invocation
|
9
|
+
|
10
|
+
def add_more_invocations
|
11
|
+
@add_more_invocations ||= []
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def add_more_after_invocation(method_name_and_args)
|
17
|
+
add_more_invocations << method_name_and_args
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#hanging_method_invocations" do
|
22
|
+
let(:client) { TestObject.new }
|
23
|
+
|
24
|
+
subject do
|
25
|
+
client.hanging_method_invocations(:add)
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with invocations" do
|
29
|
+
before do
|
30
|
+
client.add.a_method
|
31
|
+
client.add.another_method('arg1', 'arg2')
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should add invocation" do
|
35
|
+
expect(subject).to eq [[:a_method], [:another_method, 'arg1', 'arg2']]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with after_invocation" do
|
40
|
+
before do
|
41
|
+
client.add_more.a_method
|
42
|
+
client.add_more.another_method('arg1', 'arg2')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should add invocation" do
|
46
|
+
subject
|
47
|
+
expect(client.add_more_invocations).to eq [[:a_method], [:another_method, 'arg1', 'arg2']]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hanging_methods
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Pearce
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.14.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.14.0
|
46
|
+
description: Add a method that you can hang other methods.
|
47
|
+
email:
|
48
|
+
- michaelgpearce@yahoo.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- hanging_methods.gemspec
|
60
|
+
- lib/hanging_methods.rb
|
61
|
+
- lib/hanging_methods/method_call_notifier.rb
|
62
|
+
- lib/hanging_methods/version.rb
|
63
|
+
- spec/hanging_methods/method_call_notifier_spec.rb
|
64
|
+
- spec/hanging_methods_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: http://github.com/michaelgpearce/hanging_methods
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project: hanging_methods
|
87
|
+
rubygems_version: 1.8.21
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Add a method that you can hang other methods
|
91
|
+
test_files:
|
92
|
+
- spec/hanging_methods/method_call_notifier_spec.rb
|
93
|
+
- spec/hanging_methods_spec.rb
|
94
|
+
- spec/spec_helper.rb
|