hook_me_up 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/CHANGELOG.md +7 -0
- data/README.md +26 -13
- data/hook_me_up.gemspec +1 -1
- data/lib/hook_me_up.rb +37 -14
- data/lib/hook_me_up/version.rb +1 -1
- data/spec/hook_me_up_spec.rb +72 -21
- metadata +3 -3
data/.rspec
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -25,27 +25,40 @@ Or install it yourself as:
|
|
25
25
|
|
26
26
|
NOTE: `hook_me_up` call **must** come after your method definitions
|
27
27
|
|
28
|
+
```ruby
|
29
|
+
class SomeClass
|
30
|
+
include HookMeUp
|
28
31
|
|
29
|
-
|
30
|
-
|
32
|
+
def some_method
|
33
|
+
end
|
31
34
|
|
32
|
-
|
33
|
-
|
35
|
+
def some_other_method
|
36
|
+
end
|
34
37
|
|
35
|
-
|
36
|
-
|
38
|
+
def before_hook
|
39
|
+
end
|
37
40
|
|
38
|
-
|
39
|
-
|
41
|
+
def after_hook
|
42
|
+
end
|
40
43
|
|
41
|
-
|
42
|
-
|
44
|
+
hook_me_up [:some_method, :some_other_method], :before => :before_hook, :after => :after_hook
|
45
|
+
end
|
46
|
+
```
|
43
47
|
|
44
|
-
|
45
|
-
|
48
|
+
### Disabling hooks for a specific call
|
49
|
+
You can disable a hooks on specific calls with the following options:
|
50
|
+
|
51
|
+
* :no_hook
|
52
|
+
* :no_before_hook
|
53
|
+
* :no_after_hook
|
46
54
|
|
55
|
+
```ruby
|
56
|
+
some_method(args, no_hook: true) # No hooks will be called
|
57
|
+
some_method(args, no_before_hook: true) # The before hook will be skipped
|
58
|
+
some_method(args, no_after_hook: true) # The After hook will be skipped
|
59
|
+
```
|
47
60
|
|
48
|
-
###You can pass lambda to the hooks instead of methods
|
61
|
+
### You can pass lambda to the hooks instead of methods
|
49
62
|
|
50
63
|
hook_me_up :some_method, :before => lambda{ |sender, *args| sender.do_something(args) },
|
51
64
|
:after => lambda{ |sender, *args, result| sender.do_something_else(result) }
|
data/hook_me_up.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = HookMeUp::VERSION
|
9
9
|
gem.authors = ["Chen Fisher"]
|
10
10
|
gem.email = ["chen.fisher@gmail.com"]
|
11
|
-
gem.description = %q{Lets you hook any method
|
11
|
+
gem.description = %q{Lets you hook any method with :before and :after hooks}
|
12
12
|
gem.summary = %q{This gem lets you hook and method in any class with a before and after hooks}
|
13
13
|
gem.homepage = ""
|
14
14
|
|
data/lib/hook_me_up.rb
CHANGED
@@ -9,22 +9,45 @@ module HookMeUp
|
|
9
9
|
original_method = self.instance_method(method)
|
10
10
|
|
11
11
|
self.send(:define_method, method) do |*a|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
options = a.last
|
13
|
+
if options.is_a?(::Hash) && options[:no_hook] == true && a.pop
|
14
|
+
result = original_method.bind(self).call(*a)
|
15
|
+
elsif options.is_a?(::Hash) && options[:no_before_hook] == true && a.pop
|
16
|
+
result = original_method.bind(self).call(*a)
|
17
|
+
if hooks[:after]
|
18
|
+
if hooks[:after].is_a? Proc
|
19
|
+
hooks[:after].call(self, *a, result)
|
20
|
+
else
|
21
|
+
self.send(hooks[:after], *a, result)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
elsif options.is_a?(::Hash) && options[:no_after_hook] == true && a.pop
|
25
|
+
if hooks[:before]
|
26
|
+
if hooks[:before].is_a? Proc
|
27
|
+
hooks[:before].call(self, *a)
|
28
|
+
else
|
29
|
+
self.send(hooks[:before], *a)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
result = original_method.bind(self).call(*a)
|
34
|
+
else
|
35
|
+
if hooks[:before]
|
36
|
+
if hooks[:before].is_a? Proc
|
37
|
+
hooks[:before].call(self, *a)
|
38
|
+
else
|
39
|
+
self.send(hooks[:before], *a)
|
40
|
+
end
|
17
41
|
end
|
18
|
-
end
|
19
|
-
|
20
|
-
result = original_method.bind(self).call(*a)
|
21
42
|
|
43
|
+
result = original_method.bind(self).call(*a)
|
22
44
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
45
|
+
if hooks[:after]
|
46
|
+
if hooks[:after].is_a? Proc
|
47
|
+
hooks[:after].call(self, *a, result)
|
48
|
+
else
|
49
|
+
self.send(hooks[:after], *a, result)
|
50
|
+
end
|
28
51
|
end
|
29
52
|
end
|
30
53
|
|
@@ -33,7 +56,7 @@ module HookMeUp
|
|
33
56
|
end
|
34
57
|
end
|
35
58
|
end
|
36
|
-
|
59
|
+
|
37
60
|
module InstanceMethods
|
38
61
|
|
39
62
|
end
|
data/lib/hook_me_up/version.rb
CHANGED
data/spec/hook_me_up_spec.rb
CHANGED
@@ -2,12 +2,10 @@ require 'spec_helper'
|
|
2
2
|
require 'hook_me_up/sample_class'
|
3
3
|
|
4
4
|
describe HookMeUp do
|
5
|
-
|
6
|
-
@sample = SampleClass.new
|
7
|
-
end
|
5
|
+
let(:sample) { SampleClass.new }
|
8
6
|
|
9
7
|
it "hooks a before and after methods" do
|
10
|
-
class <<
|
8
|
+
class << sample
|
11
9
|
include HookMeUp
|
12
10
|
|
13
11
|
hook_me_up :method_one, :before => :before_hook,
|
@@ -20,14 +18,14 @@ describe HookMeUp do
|
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
23
|
-
|
24
|
-
|
21
|
+
sample.should_receive(:before_hook).with('args')
|
22
|
+
sample.should_receive(:after_hook).with('args', 'args')
|
25
23
|
|
26
|
-
|
24
|
+
sample.method_one('args').should eq 'args'
|
27
25
|
end
|
28
26
|
|
29
27
|
it "can receive a block as a before or after hooks" do
|
30
|
-
class <<
|
28
|
+
class << sample
|
31
29
|
include HookMeUp
|
32
30
|
|
33
31
|
attr_accessor :before, :after
|
@@ -36,15 +34,15 @@ describe HookMeUp do
|
|
36
34
|
:after => lambda{ |sender, *args, result| sender.after = true }
|
37
35
|
end
|
38
36
|
|
39
|
-
|
37
|
+
sample.method_one('args').should eq 'args'
|
40
38
|
|
41
|
-
|
42
|
-
|
39
|
+
sample.before.should eq true
|
40
|
+
sample.after.should eq true
|
43
41
|
end
|
44
42
|
|
45
43
|
|
46
44
|
it "can receive multiple methods to hook" do
|
47
|
-
class <<
|
45
|
+
class << sample
|
48
46
|
include HookMeUp
|
49
47
|
|
50
48
|
attr_accessor :before, :after
|
@@ -54,19 +52,19 @@ describe HookMeUp do
|
|
54
52
|
def before_hook(*args)
|
55
53
|
end
|
56
54
|
|
57
|
-
def after_hook(*args
|
55
|
+
def after_hook(*args)
|
58
56
|
end
|
59
57
|
end
|
60
58
|
|
61
|
-
|
62
|
-
|
59
|
+
sample.should_receive(:before_hook).exactly(2)
|
60
|
+
sample.should_receive(:after_hook).exactly(2)
|
63
61
|
|
64
|
-
|
65
|
-
|
62
|
+
sample.method_one('args').should eq 'args'
|
63
|
+
sample.method_two.should eq 'method_two'
|
66
64
|
end
|
67
65
|
|
68
66
|
it "should not call before or after if not defined" do
|
69
|
-
class <<
|
67
|
+
class << sample
|
70
68
|
include HookMeUp
|
71
69
|
|
72
70
|
hook_me_up :method_one, :before => :before_hook
|
@@ -78,9 +76,62 @@ describe HookMeUp do
|
|
78
76
|
end
|
79
77
|
end
|
80
78
|
|
81
|
-
|
82
|
-
|
79
|
+
sample.should_receive(:before_hook).with('args')
|
80
|
+
sample.should_not_receive(:after_hook)
|
81
|
+
|
82
|
+
sample.method_one('args').should eq 'args'
|
83
|
+
end
|
84
|
+
|
85
|
+
context "when disabled" do
|
86
|
+
before do
|
87
|
+
class << sample
|
88
|
+
include HookMeUp
|
89
|
+
|
90
|
+
hook_me_up :method_one, :before => :before_hook, :after => :after_hook
|
91
|
+
|
92
|
+
def before_hook(*args)
|
93
|
+
end
|
94
|
+
|
95
|
+
def after_hook(*args)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "no_hook" do
|
101
|
+
it "doesn't call before hook" do
|
102
|
+
sample.should_not_receive(:after_hook)
|
103
|
+
sample.method_one('args', no_hook: true).should eq 'args'
|
104
|
+
end
|
105
|
+
|
106
|
+
it "doesn't call after hook" do
|
107
|
+
sample.should_not_receive(:after_hook)
|
108
|
+
sample.method_one('args', no_hook: true).should eq 'args'
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "no_before_hook" do
|
113
|
+
it "doesn't call before hook" do
|
114
|
+
sample.should_not_receive(:before_hook)
|
115
|
+
sample.method_one('args', no_before_hook: true).should eq 'args'
|
116
|
+
end
|
117
|
+
|
118
|
+
it "calls the after hook" do
|
119
|
+
sample.should_receive(:after_hook)
|
120
|
+
sample.method_one('args')
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "no_after_hook" do
|
125
|
+
it "doesn't call after hook" do
|
126
|
+
sample.should_not_receive(:after_hook).with('args', 'args')
|
127
|
+
sample.method_one('args', no_after_hook: true).should eq 'args'
|
128
|
+
end
|
129
|
+
|
130
|
+
it "calls the before hook" do
|
131
|
+
sample.should_receive(:before_hook)
|
132
|
+
sample.method_one('args')
|
133
|
+
end
|
134
|
+
end
|
83
135
|
|
84
|
-
@sample.method_one('args').should eq 'args'
|
85
136
|
end
|
86
137
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hook_me_up
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -27,7 +27,7 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
-
description: Lets you hook any method
|
30
|
+
description: Lets you hook any method with :before and :after hooks
|
31
31
|
email:
|
32
32
|
- chen.fisher@gmail.com
|
33
33
|
executables: []
|