method_callbacks 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -0
- data/README.md +27 -0
- data/lib/method_callbacks/executor.rb +13 -4
- data/lib/method_callbacks/version.rb +1 -1
- data/lib/method_callbacks.rb +7 -4
- data/spec/method_callbacks_spec.rb +36 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19ee03dc720d25d9e1e326ac91e9718dd9e01272
|
4
|
+
data.tar.gz: f61c75e0258697e042aece4916c77c1c3de919a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 593b6df610d9e64695f9fe55fa185e943217f0b56775d993880398dbc6b320e8963f6fef04b4b1b811a155ea0d977787f63cf07f8fafa32e899e7faa7200c510
|
7
|
+
data.tar.gz: 11802ed240eb4727516519fed47f1d10fc70c085ef54462eded418eb1cdbcdbc95df08c98f21c47807ed5f78246d9e4845d838eb81cdb515f64479630fb97b55
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -91,6 +91,33 @@ Executing farewell after action but before unload!
|
|
91
91
|
Executing unload after action and farewell!
|
92
92
|
Executing block callback after action and everything else!
|
93
93
|
=> "My return value!"
|
94
|
+
|
95
|
+
class TestProxyResult
|
96
|
+
include MethodCallbacks
|
97
|
+
|
98
|
+
def result
|
99
|
+
"hello!"
|
100
|
+
end
|
101
|
+
proxy_result(:result) { |original_result| "the original result was: #{original_result}" }
|
102
|
+
|
103
|
+
def result_with_block
|
104
|
+
"hello world!"
|
105
|
+
end
|
106
|
+
proxy_result(:result_with_block) { |original_result, &block| block.call(original_result) }
|
107
|
+
|
108
|
+
def chain_result_proxy
|
109
|
+
"original"
|
110
|
+
end
|
111
|
+
proxy_result(:chain_result_proxy) { |original_result| "original_result: #{original_result}" }
|
112
|
+
proxy_result(:chain_result_proxy) { |chained_result| "chained_result: #{chained_result}" }
|
113
|
+
end
|
114
|
+
|
115
|
+
> TestProxyResult.new.result
|
116
|
+
=> "the original result was: hello!"
|
117
|
+
> TestProxyResult.new.result_with_block
|
118
|
+
=> "hello world!"
|
119
|
+
> TestProxyResult.new.chain_result_proxy
|
120
|
+
=> "original_result: chained_result: original"
|
94
121
|
```
|
95
122
|
|
96
123
|
## Contributing
|
@@ -8,10 +8,15 @@ module MethodCallbacks
|
|
8
8
|
@object = object
|
9
9
|
end
|
10
10
|
|
11
|
-
def execute
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def execute(&block)
|
12
|
+
case type
|
13
|
+
when :proxy
|
14
|
+
execute_proxy_callbacks(&block)
|
15
|
+
when :around
|
16
|
+
execute_around_callbacks(&block)
|
17
|
+
else
|
18
|
+
execute_callbacks
|
19
|
+
end
|
15
20
|
end
|
16
21
|
|
17
22
|
private
|
@@ -27,5 +32,9 @@ module MethodCallbacks
|
|
27
32
|
def execute_callbacks
|
28
33
|
callbacks.each { |callback_name| callback_name.is_a?(Proc) ? object.instance_eval(&callback_name) : object.send(callback_name) }
|
29
34
|
end
|
35
|
+
|
36
|
+
def execute_proxy_callbacks(&block_on_call)
|
37
|
+
callbacks.reduce(object) { |result, block| block.call(result, &block_on_call) }
|
38
|
+
end
|
30
39
|
end
|
31
40
|
end
|
data/lib/method_callbacks.rb
CHANGED
@@ -16,6 +16,10 @@ module MethodCallbacks
|
|
16
16
|
define(method_name, :around, callback_names)
|
17
17
|
end
|
18
18
|
|
19
|
+
def proxy_result(method_name, &block)
|
20
|
+
define_with_block(method_name, :proxy, &block)
|
21
|
+
end
|
22
|
+
|
19
23
|
def before_method(method_name, *callback_names)
|
20
24
|
define(method_name, :before, callback_names)
|
21
25
|
define_with_block(method_name, :before, &Proc.new) if block_given?
|
@@ -57,11 +61,10 @@ module MethodCallbacks
|
|
57
61
|
|
58
62
|
method.lock! && alias_method(method.alias, method.name)
|
59
63
|
|
60
|
-
define_method(method.name) do
|
64
|
+
define_method(method.name) do |&block|
|
61
65
|
method.execute(:before, self)
|
62
|
-
return_value = method.execute(:around, self)
|
63
|
-
|
64
|
-
end
|
66
|
+
return_value = method.execute(:around, self) { send(method.alias) }
|
67
|
+
return_value = method.execute(:proxy, return_value, &block)
|
65
68
|
method.execute(:after, self)
|
66
69
|
return_value
|
67
70
|
end
|
@@ -2,6 +2,7 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe MethodCallbacks do
|
4
4
|
let(:test_callbacks) { TestCallbacks.new }
|
5
|
+
let(:test_proxy_result) { TestProxyResult.new }
|
5
6
|
|
6
7
|
it "should execute all the callbacks on action" do
|
7
8
|
expect(test_callbacks).to receive(:puts).with("Executing block").ordered
|
@@ -18,6 +19,21 @@ describe MethodCallbacks do
|
|
18
19
|
|
19
20
|
expect(test_callbacks.action).to eq("Return value")
|
20
21
|
end
|
22
|
+
|
23
|
+
it "should proxy the result" do
|
24
|
+
expect(test_proxy_result.result).to eq("the original result was: hello!")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should proxy the result with block" do
|
28
|
+
test_string = "the original result was:"
|
29
|
+
expect(test_proxy_result.result_with_block do |result|
|
30
|
+
"#{test_string} #{result}"
|
31
|
+
end).to eq("#{test_string} hello world!")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should chain proxy results" do
|
35
|
+
expect(test_proxy_result.chain_result_proxy).to eq("chained_result: original_result: original")
|
36
|
+
end
|
21
37
|
end
|
22
38
|
|
23
39
|
class TestCallbacks
|
@@ -70,3 +86,23 @@ class TestCallbacks
|
|
70
86
|
return_value
|
71
87
|
end
|
72
88
|
end
|
89
|
+
|
90
|
+
class TestProxyResult
|
91
|
+
include MethodCallbacks
|
92
|
+
|
93
|
+
def result
|
94
|
+
"hello!"
|
95
|
+
end
|
96
|
+
proxy_result(:result) { |original_result| "the original result was: #{original_result}" }
|
97
|
+
|
98
|
+
def result_with_block
|
99
|
+
"hello world!"
|
100
|
+
end
|
101
|
+
proxy_result(:result_with_block) { |original_result, &block| block.call(original_result) }
|
102
|
+
|
103
|
+
def chain_result_proxy
|
104
|
+
"original"
|
105
|
+
end
|
106
|
+
proxy_result(:chain_result_proxy) { |original_result| "original_result: #{original_result}" }
|
107
|
+
proxy_result(:chain_result_proxy) { |chained_result| "chained_result: #{chained_result}" }
|
108
|
+
end
|