method_hooks 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/method_hooks/version.rb +1 -1
- data/lib/method_hooks.rb +23 -0
- data/method_hooks.gemspec +2 -0
- data/spec/lib/method_hooks_spec.rb +77 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7e454d3f08fdcc2ee62ede53b2b570decdc8e93
|
4
|
+
data.tar.gz: cb9bf2fc3b04f9b10c65df6b2a3cde887cdf392d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18014051b5fc06b6cbd3e2181b32f46c2902a26b30da7c7309135dc235c20185b39a73650a2517f551ca9aecc7a52c88d37d10cceb438e05ac23a790c8b3505c
|
7
|
+
data.tar.gz: ed644fb295f63dbe1d819c686dd68d6d74d0d6d5e77eb7ccfa5a06a042d9543328ecd25a43193c7fe85a5b71960a4357ba5c4d6c4a07686c2888ae22f9d569fc
|
data/lib/method_hooks/version.rb
CHANGED
data/lib/method_hooks.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require "method_hooks/version"
|
2
|
+
require "active_support/core_ext/class/subclasses"
|
3
|
+
require "active_support/core_ext/object/deep_dup"
|
2
4
|
|
3
5
|
module MethodHooks
|
4
6
|
@new_method = true
|
@@ -9,6 +11,15 @@ module MethodHooks
|
|
9
11
|
|
10
12
|
private
|
11
13
|
|
14
|
+
def inherited(child_class)
|
15
|
+
child_class.instance_variable_set(:@new_method, true)
|
16
|
+
child_class.instance_variable_set(:@before_callbacks, before_callbacks.deep_dup)
|
17
|
+
child_class.instance_variable_set(:@around_callbacks, around_callbacks.deep_dup)
|
18
|
+
child_class.instance_variable_set(:@after_callbacks, after_callbacks.deep_dup)
|
19
|
+
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
12
23
|
def method_added(method_name)
|
13
24
|
super
|
14
25
|
return if @new_method == false || [:initialize, :call_before_callbacks, :call_around_callbacks, :call_after_callbacks].include?(method_name)
|
@@ -45,18 +56,30 @@ module MethodHooks
|
|
45
56
|
method_names.each do |method_name|
|
46
57
|
before_callbacks[method_name] << block
|
47
58
|
end
|
59
|
+
|
60
|
+
subclasses.each do |subclass|
|
61
|
+
descendant.before(method_names, &block)
|
62
|
+
end
|
48
63
|
end
|
49
64
|
|
50
65
|
def around(*method_names, &block)
|
51
66
|
method_names.each do |method_name|
|
52
67
|
around_callbacks[method_name] << block
|
53
68
|
end
|
69
|
+
|
70
|
+
subclasses.each do |subclass|
|
71
|
+
descendant.around(method_names, &block)
|
72
|
+
end
|
54
73
|
end
|
55
74
|
|
56
75
|
def after(*method_names, &block)
|
57
76
|
method_names.each do |method_name|
|
58
77
|
after_callbacks[method_name] << block
|
59
78
|
end
|
79
|
+
|
80
|
+
subclasses.each do |subclass|
|
81
|
+
subclass.after(method_names, &block)
|
82
|
+
end
|
60
83
|
end
|
61
84
|
|
62
85
|
module InstanceMethods
|
data/method_hooks.gemspec
CHANGED
@@ -103,4 +103,81 @@ describe MethodHooks do
|
|
103
103
|
expect(Base._methods).to include(:some_method)
|
104
104
|
end
|
105
105
|
|
106
|
+
describe 'inheritance' do
|
107
|
+
before do
|
108
|
+
Object.send(:remove_const, :Child) if Object.const_defined?(:Child)
|
109
|
+
Object.send(:remove_const, :Grandchild) if Object.const_defined?(:Grandchild)
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should pass parent hooks to the child' do
|
113
|
+
Base.instance_eval do
|
114
|
+
before(:save) { @events << 'before' }
|
115
|
+
|
116
|
+
around(:save) do |method|
|
117
|
+
@events << 'before_around'
|
118
|
+
method.call
|
119
|
+
@events << 'after_around'
|
120
|
+
end
|
121
|
+
|
122
|
+
after(:save) { @events << 'after' }
|
123
|
+
end
|
124
|
+
|
125
|
+
class Child < Base; end
|
126
|
+
|
127
|
+
child = Child.new
|
128
|
+
child.save
|
129
|
+
|
130
|
+
expect(child.events).to eq(['before', 'before_around', 'save', 'after_around', 'after'])
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'should not mutate the parent callbacks when a callback is added to a child' do
|
134
|
+
Base.instance_eval do
|
135
|
+
before(:save) { @events << 'parent before' }
|
136
|
+
end
|
137
|
+
|
138
|
+
class Child < Base; end
|
139
|
+
|
140
|
+
Child.instance_eval do
|
141
|
+
before(:save) { @events << 'child before' }
|
142
|
+
end
|
143
|
+
|
144
|
+
child = Child.new
|
145
|
+
child.save
|
146
|
+
parent = Base.new
|
147
|
+
parent.save
|
148
|
+
|
149
|
+
expect(child.events).to eq(['parent before', 'child before', 'save'])
|
150
|
+
expect(parent.events).to eq(['parent before', 'save'])
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should add callbacks to all descendents of a class, recursively' do
|
154
|
+
Base.instance_eval do
|
155
|
+
before(:save) { @events << 'parent before' }
|
156
|
+
end
|
157
|
+
|
158
|
+
class Child < Base; end
|
159
|
+
|
160
|
+
Child.instance_eval do
|
161
|
+
before(:save) { @events << 'child before' }
|
162
|
+
end
|
163
|
+
|
164
|
+
class Grandchild < Child; end
|
165
|
+
|
166
|
+
Grandchild.instance_eval do
|
167
|
+
before(:save) { @events << 'grandchild before' }
|
168
|
+
end
|
169
|
+
|
170
|
+
parent = Base.new
|
171
|
+
parent.save
|
172
|
+
child = Child.new
|
173
|
+
child.save
|
174
|
+
grandchild = Grandchild.new
|
175
|
+
grandchild.save
|
176
|
+
|
177
|
+
|
178
|
+
expect(parent.events).to eq(['parent before', 'save'])
|
179
|
+
expect(child.events).to eq(['parent before', 'child before', 'save'])
|
180
|
+
expect(grandchild.events).to eq(['parent before', 'child before', 'grandchild before', 'save'])
|
181
|
+
end
|
182
|
+
end
|
106
183
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_hooks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frank Bonetti
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 3.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.1'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.1'
|
55
69
|
description: Rails-style method hooks for plain old Ruby objects
|
56
70
|
email:
|
57
71
|
- frank.r.bonetti@gmail.com
|