method_callbacks 1.0.0 → 1.1.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 +9 -0
- data/README.md +4 -0
- data/lib/method_callbacks.rb +6 -6
- data/lib/method_callbacks/method.rb +6 -0
- data/lib/method_callbacks/version.rb +1 -1
- data/spec/method_callbacks_spec.rb +25 -27
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ce34ba7f8257e2369d5fc112aa947a7455e58d6
|
4
|
+
data.tar.gz: a929db6bcd8cc31b78f797158aabb24e22722151
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e692481c23bf044c38a0c2b59fca6747454d5c8d131d49543ede7a023739d0a73642b3a2ea7cf01eb276d91c2c6e28896387834f7ab549d95b6b2bb67bf6c6d
|
7
|
+
data.tar.gz: ff58f30bde12591cbaf329fadac637d9d2f860e2c5c98e594c799a22bc3502f6cceb8054cac6d7878cf873c72a9859e5d31d09d52a9cafde3f7eb038ef934143
|
data/CHANGELOG
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
1.1.0 / 2014-06-12
|
2
|
+
* Add badges for Travis CI and RubyGems to README
|
3
|
+
* Test return value of method with callbacks
|
4
|
+
* Encapsulate a method's alias in the method class
|
5
|
+
* Fix defining callbacks after the method definition
|
6
|
+
* Add changelog
|
7
|
+
|
8
|
+
1.0.0 / 2014-05-31
|
9
|
+
* Initial release of method_callbacks
|
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[](https://travis-ci.org/MorganShowman/method_callbacks)
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/method_callbacks)
|
4
|
+
|
1
5
|
# MethodCallbacks
|
2
6
|
|
3
7
|
Add after, around, and before callbacks to your methods.
|
data/lib/method_callbacks.rb
CHANGED
@@ -2,8 +2,6 @@ require "method_callbacks/finder"
|
|
2
2
|
require "method_callbacks/version"
|
3
3
|
|
4
4
|
module MethodCallbacks
|
5
|
-
ALIAS_PREFIX = "__method_callback_alias_to_original"
|
6
|
-
|
7
5
|
def self.included(base)
|
8
6
|
base.extend(ClassMethods)
|
9
7
|
end
|
@@ -43,7 +41,9 @@ module MethodCallbacks
|
|
43
41
|
end
|
44
42
|
|
45
43
|
def find_or_new(method_name)
|
46
|
-
finder(method_name).find_or_new
|
44
|
+
method = finder(method_name).find_or_new
|
45
|
+
redefine_method(method)
|
46
|
+
method
|
47
47
|
end
|
48
48
|
|
49
49
|
def method_added(method_name)
|
@@ -53,14 +53,14 @@ module MethodCallbacks
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def redefine_method(method)
|
56
|
-
return if !method || method.locked?
|
56
|
+
return if !method || method.locked? || !method_defined?(method.name) || method_defined?(method.alias)
|
57
57
|
|
58
|
-
method.lock! && alias_method(
|
58
|
+
method.lock! && alias_method(method.alias, method.name)
|
59
59
|
|
60
60
|
define_method(method.name) do
|
61
61
|
method.execute(:before, self)
|
62
62
|
return_value = method.execute(:around, self) do
|
63
|
-
send(
|
63
|
+
send(method.alias)
|
64
64
|
end
|
65
65
|
method.execute(:after, self)
|
66
66
|
return_value
|
@@ -7,6 +7,8 @@ module MethodCallbacks
|
|
7
7
|
class Method
|
8
8
|
extend Forwardable
|
9
9
|
|
10
|
+
ALIAS_PREFIX = "__method_callback_alias_to_original"
|
11
|
+
|
10
12
|
attr_reader :name
|
11
13
|
|
12
14
|
def_delegators :definer, :define, :define_with_block
|
@@ -20,6 +22,10 @@ module MethodCallbacks
|
|
20
22
|
self.name == other.name
|
21
23
|
end
|
22
24
|
|
25
|
+
def alias
|
26
|
+
"#{ALIAS_PREFIX}_#{name}"
|
27
|
+
end
|
28
|
+
|
23
29
|
def callbacks(type)
|
24
30
|
@_callbacks ||= {}
|
25
31
|
@_callbacks[type] ||= []
|
@@ -16,16 +16,19 @@ describe MethodCallbacks do
|
|
16
16
|
expect(test_callbacks).to receive(:puts).with("Executing unload").ordered
|
17
17
|
expect(test_callbacks).to receive(:puts).with("Executing block").ordered
|
18
18
|
|
19
|
-
test_callbacks.action
|
19
|
+
expect(test_callbacks.action).to eq("Return value")
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
class TestCallbacks
|
24
24
|
include MethodCallbacks
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
def intro
|
27
|
+
puts "Executing intro"
|
28
|
+
end
|
29
|
+
|
30
|
+
def greet
|
31
|
+
puts "Executing greet"
|
29
32
|
end
|
30
33
|
|
31
34
|
before_method :action do
|
@@ -33,8 +36,23 @@ class TestCallbacks
|
|
33
36
|
end
|
34
37
|
before_method :action, :intro
|
35
38
|
before_method :action, :greet
|
39
|
+
def action
|
40
|
+
puts "Executing action"
|
36
41
|
|
37
|
-
|
42
|
+
"Return value"
|
43
|
+
end
|
44
|
+
after_method :action, :farewell, :unload
|
45
|
+
after_method :action do
|
46
|
+
puts "Executing block"
|
47
|
+
end
|
48
|
+
|
49
|
+
def farewell
|
50
|
+
puts "Executing farewell"
|
51
|
+
end
|
52
|
+
|
53
|
+
def unload
|
54
|
+
puts "Executing unload"
|
55
|
+
end
|
38
56
|
|
39
57
|
def outer_around
|
40
58
|
puts "Executing pre outer_around"
|
@@ -43,32 +61,12 @@ class TestCallbacks
|
|
43
61
|
return_value
|
44
62
|
end
|
45
63
|
|
64
|
+
around_method :action, :outer_around, :inner_around
|
65
|
+
|
46
66
|
def inner_around
|
47
67
|
puts "Executing pre inner_around"
|
48
68
|
return_value = yield
|
49
69
|
puts "Executing post inner_around"
|
50
70
|
return_value
|
51
71
|
end
|
52
|
-
|
53
|
-
def action
|
54
|
-
puts "Executing action"
|
55
|
-
|
56
|
-
"Return value"
|
57
|
-
end
|
58
|
-
|
59
|
-
def greet
|
60
|
-
puts "Executing greet"
|
61
|
-
end
|
62
|
-
|
63
|
-
def intro
|
64
|
-
puts "Executing intro"
|
65
|
-
end
|
66
|
-
|
67
|
-
def farewell
|
68
|
-
puts "Executing farewell"
|
69
|
-
end
|
70
|
-
|
71
|
-
def unload
|
72
|
-
puts "Executing unload"
|
73
|
-
end
|
74
72
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_callbacks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Morgan Showman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -89,6 +89,7 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- .gitignore
|
91
91
|
- .travis.yml
|
92
|
+
- CHANGELOG
|
92
93
|
- Gemfile
|
93
94
|
- Guardfile
|
94
95
|
- LICENSE.txt
|