after_do 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +12 -2
- data/CHANGELOG.md +14 -0
- data/Gemfile +1 -6
- data/README.md +41 -20
- data/lib/after_do.rb +30 -22
- data/lib/after_do/version.rb +1 -1
- data/samples/getting_a_hold.rb +34 -11
- data/samples/singleton.rb +7 -1
- data/samples/with_module.rb +17 -3
- data/spec/after_do_spec.rb +54 -10
- data/spec/spec_helper.rb +2 -4
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cfdaaaac12918f8718cd7a32b92cd9c5efc3afe
|
4
|
+
data.tar.gz: ab0e670c82a920ba41e7e5929cb25cc5d56a214b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8be211216e9f15df0992184aee7159d4c03dc01d1ab8b0c97c823c6a9d64fb885856027286254d8f7d757090eb615489180d9557eb659afdc239fffe3e30ae2
|
7
|
+
data.tar.gz: 1659730491ed28c4445d88629aee0e15e5da21871dca4425e0ae8b4a457e0faff28da45b4a953d4846b8183c56c23ac0c5a9c2905c471d0517c4a2499335e352
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.3.0
|
data/.travis.yml
CHANGED
@@ -1,8 +1,18 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
-
|
4
|
-
- jruby-19mode
|
3
|
+
- jruby
|
5
4
|
- 1.9.3
|
6
5
|
- 2.0
|
7
6
|
- 2.1
|
7
|
+
- 2.2
|
8
|
+
- 2.3.0
|
9
|
+
- jruby-9.0.5.0
|
10
|
+
- jruby-head
|
11
|
+
matrix:
|
12
|
+
allow_failures:
|
13
|
+
- rvm: jruby-head
|
14
|
+
sudo: false
|
15
|
+
cache: bundler
|
16
|
+
before_script:
|
17
|
+
- "bundle update"
|
8
18
|
script: bundle exec rspec spec
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
## 0.4.0 (March 25th, 2015)
|
2
|
+
|
3
|
+
* Hand new arguments to callbacks, namely method name and return value (for after). New Order of block arguments for before is: `arguments, method_name, object`, for after it is: `arguments, method_name, return_value, object`. To migrate to this version you need to change blocks that either use `*args` and then access `args` or calls that do `arg_1, arg_2, object` as the third argument is now the method name. You can change them to `arg_1, arg_2, *, object`.
|
4
|
+
* Dropped explicit rubinius support, it should work but dropped testing. If you need it please get in touch.
|
5
|
+
|
6
|
+
## 0.3.1 (March 27th, 2014)
|
7
|
+
|
8
|
+
* improve error reporting
|
9
|
+
* run warning free with -w
|
10
|
+
|
11
|
+
## 0.3.0 (January 19th, 2014)
|
12
|
+
|
13
|
+
* Work properly with inheritance (callbacks are just called if `super` is really invoked)
|
14
|
+
* Work properly with modules (callbacks on module methods are executed when the methods are called)
|
data/Gemfile
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
# Specify your gem's dependencies in after_do.gemspec
|
4
3
|
gemspec
|
5
4
|
|
6
|
-
gem '
|
7
|
-
gem 'rubinius-coverage', platform: :rbx
|
8
|
-
gem 'json', platform: :rbx
|
9
|
-
|
10
|
-
gem 'bundler', '~> 1.3'
|
5
|
+
gem 'bundler'
|
11
6
|
gem 'rake'
|
12
7
|
gem 'rspec'
|
13
8
|
gem 'guard-rspec'
|
data/README.md
CHANGED
@@ -6,6 +6,8 @@ If the class extends `AfterDo` you can simply do this by
|
|
6
6
|
|
7
7
|
```
|
8
8
|
MyClass.after :some_method do whatever_you_want end
|
9
|
+
# you can also do before
|
10
|
+
MyClass.before :a_method do so_much end
|
9
11
|
```
|
10
12
|
|
11
13
|
Some facts about after_do:
|
@@ -36,7 +38,7 @@ And then execute:
|
|
36
38
|
Or install it yourself as:
|
37
39
|
|
38
40
|
$ gem install after_do
|
39
|
-
|
41
|
+
|
40
42
|
And then you have to require the gem before you can use it:
|
41
43
|
|
42
44
|
require 'after_do'
|
@@ -79,7 +81,7 @@ dog2.bark
|
|
79
81
|
# I just heard a dog bark!
|
80
82
|
```
|
81
83
|
|
82
|
-
### Getting a hold of the method arguments
|
84
|
+
### Getting a hold of the method arguments, the object and more!
|
83
85
|
|
84
86
|
With after_do both the arguments to the method you are attaching the callback to and the object for which the callback is executed are passed into the callback block.
|
85
87
|
|
@@ -89,19 +91,47 @@ So if you have a method that takes two arguments you can get those like this:
|
|
89
91
|
MyClass.after :two_arg_method do |argument_one, argument_2| something end
|
90
92
|
```
|
91
93
|
|
92
|
-
The
|
94
|
+
The method name is passed in as the third argument:
|
93
95
|
|
94
96
|
```ruby
|
95
|
-
MyClass.
|
97
|
+
MyClass.before :two_arg_method do |argument_one, argument_2, method_name|
|
98
|
+
something
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
The method name is passed for convenience if you do logging for instance. If you start doing `if` or `case` on the method name, rather think about if you should use different callbacks for the different methods.
|
103
|
+
|
104
|
+
Another argument that is passed in, but only for `after` is the return value of the method so you can do:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
MyClass.after :two_arg_method do |arg1, arg2, name, return_value|
|
108
|
+
report return_value
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
The object itself is passed in as the last block argument finally so all arguments together are:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
MyClass.after :method do |arg1, arg2, name, return_value, object|
|
116
|
+
magic
|
117
|
+
end
|
118
|
+
|
119
|
+
# remember before doesn't have the return value
|
120
|
+
MyClass.before :method do |arg1, arg2, name, object|
|
121
|
+
other_magic
|
122
|
+
end
|
96
123
|
```
|
97
124
|
|
98
|
-
|
125
|
+
Why this order of arguments? Well at first there are the arguments related to the method itself (arguments, name and optional return value) and then there is the object.
|
126
|
+
|
127
|
+
Of course you can just grab the object, if you're not interested in all the method related data, by using the splat operator to _soak up_ all the other arguments:
|
99
128
|
|
100
129
|
```ruby
|
101
|
-
MyClass.after :two_arg_method do
|
130
|
+
MyClass.after :two_arg_method do |*, obj| fancy_stuff(obj) end
|
102
131
|
```
|
103
132
|
|
104
|
-
|
133
|
+
|
134
|
+
If you do not want to get a hold of the method arguments or the object, then you can just don't care about the block parameters and can leave them out :-)
|
105
135
|
|
106
136
|
Check out the [getting a hold sample](https://github.com/PragTob/after_do/blob/master/samples/getting_a_hold.rb) for more.
|
107
137
|
|
@@ -174,7 +204,7 @@ after_do works with modules just like it works with classes from version 0.3.0 o
|
|
174
204
|
class MyClass
|
175
205
|
include MyModule
|
176
206
|
# ....
|
177
|
-
end
|
207
|
+
end
|
178
208
|
|
179
209
|
MyModule.extend AfterDo
|
180
210
|
MyModule.after :some_method do cool_stuff end
|
@@ -252,17 +282,6 @@ return_value
|
|
252
282
|
|
253
283
|
To do this some helper methods are defined in the AfterDo module. As classes have to extend the AfterDo module all the methods that you are not supposed to call yourself are prefixed with `_after_do_` to minimize the risk of method name clashes. The only not prefixed method are `after`, `before` and `remove_all_callbacks`.
|
254
284
|
|
255
|
-
|
256
|
-
## Is there a before method?
|
257
|
-
|
258
|
-
Yes. It works just like the `after` method, but the callbacks are executed before the original method is called. You can also mix and match before and after calls.
|
259
|
-
|
260
|
-
```ruby
|
261
|
-
MyClass.before :a_method do so_much end
|
262
|
-
```
|
263
|
-
|
264
|
-
Check out the [before sample](https://github.com/PragTob/after_do/blob/master/samples/before.rb).
|
265
|
-
|
266
285
|
### Method granularity
|
267
286
|
|
268
287
|
after_do works on the granularity of methods. That means that you can only attach callbacks to methods. This is no problem however, since if it's your code you can always define new methods. E.g. if you want to attach callbacks to the end of some operation that happens in the middle of a method just define a new method for that piece of code.
|
@@ -288,7 +307,9 @@ A use case I feel this is particularly made for is redrawing. That's what we use
|
|
288
307
|
|
289
308
|
## Does it work with Ruby interpreter X?
|
290
309
|
|
291
|
-
Thanks to the awesome [travis CI](https://travis-ci.org/) the specs are run with
|
310
|
+
Thanks to the awesome [travis CI](https://travis-ci.org/) the specs are run with CRuby 1.9.3, 2.0, 2.1, 2.2, 2.3 and the latest JRuby releases. It _should_ work with rubinius, but some problems lead me to remove it from the build matrix.
|
311
|
+
|
312
|
+
So in short, this should work with all of them and is aimed at doing so :-)
|
292
313
|
|
293
314
|
## Contributing
|
294
315
|
|
data/lib/after_do.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
# The after_do library
|
2
|
-
# with any class,
|
1
|
+
# The after_do library adds callbacks to methods in Ruby. Before using this
|
2
|
+
# with any class, said class has to extend the AfterDo module first e.g.
|
3
3
|
# MyClass.extend AfterDo
|
4
4
|
# MyClass.after :some_method do awesome_stuff end
|
5
5
|
#
|
@@ -12,7 +12,7 @@ module AfterDo
|
|
12
12
|
class NonExistingMethodError < StandardError ; end
|
13
13
|
|
14
14
|
# Raised when an error occurs in one of the callbacks of a method. Provides
|
15
|
-
# additional information as
|
15
|
+
# additional information such as which method and where the block was
|
16
16
|
# defined.
|
17
17
|
class CallbackError < StandardError ; end
|
18
18
|
|
@@ -21,18 +21,18 @@ module AfterDo
|
|
21
21
|
# MyClass.after :some_method do awesome_stuff end
|
22
22
|
# It can only take a list of methods after which a block should be executed:
|
23
23
|
# MyClass.after :method1, :method2, :method3 do puts 'jay!' end
|
24
|
-
# The list
|
24
|
+
# The list can also be an Array.
|
25
25
|
def after(*methods, &block)
|
26
26
|
_after_do_define_callback(:after, methods, block)
|
27
27
|
end
|
28
28
|
|
29
|
-
# This method works much like .after -
|
29
|
+
# This method works much like .after - except blocks are executed
|
30
30
|
# before the method is called.
|
31
31
|
def before(*methods, &block)
|
32
32
|
_after_do_define_callback(:before, methods, block)
|
33
33
|
end
|
34
34
|
|
35
|
-
# Removes all callbacks
|
35
|
+
# Removes all callbacks attached to methods in the module.
|
36
36
|
def remove_all_callbacks
|
37
37
|
@_after_do_callbacks = _after_do_basic_hash
|
38
38
|
end
|
@@ -40,7 +40,7 @@ module AfterDo
|
|
40
40
|
private
|
41
41
|
def _after_do_define_callback(type, methods, block)
|
42
42
|
@_after_do_callbacks ||= _after_do_basic_hash
|
43
|
-
methods = methods.flatten #in case someone used an Array
|
43
|
+
methods = methods.flatten # in case someone used an Array
|
44
44
|
_after_do_raise_no_method_specified(type) if methods.empty?
|
45
45
|
methods.each do |method|
|
46
46
|
_after_do_add_callback_to_method(type, method, block)
|
@@ -60,17 +60,17 @@ module AfterDo
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def _after_do_add_callback_to_method(type, method, block)
|
63
|
-
|
63
|
+
alias_name = _after_do_aliased_name method
|
64
|
+
_after_do_redefine(method, alias_name) unless _after_do_redefined?(alias_name)
|
64
65
|
@_after_do_callbacks[type][method] << block
|
65
66
|
end
|
66
67
|
|
67
|
-
def
|
68
|
-
private_instance_methods(false).include?
|
68
|
+
def _after_do_redefined?(alias_name)
|
69
|
+
private_instance_methods(false).include? alias_name
|
69
70
|
end
|
70
71
|
|
71
|
-
def
|
72
|
+
def _after_do_redefine(method, alias_name)
|
72
73
|
_after_do_raise_no_method_error(method) unless _after_do_defined?(method)
|
73
|
-
alias_name = _after_do_aliased_name method
|
74
74
|
_after_do_rename_old_method(method, alias_name)
|
75
75
|
_after_do_redefine_method_with_callback(method, alias_name)
|
76
76
|
end
|
@@ -95,24 +95,32 @@ module AfterDo
|
|
95
95
|
def _after_do_redefine_method_with_callback(method, alias_name)
|
96
96
|
callback_klazz = self
|
97
97
|
define_method method do |*args|
|
98
|
-
callback_klazz.send(:
|
98
|
+
callback_klazz.send(:_after_do_execute_before_callbacks, method, self, *args)
|
99
99
|
return_value = send(alias_name, *args)
|
100
|
-
callback_klazz.send(:
|
100
|
+
callback_klazz.send(:_after_do_execute_after_callbacks, method, self, *args, return_value)
|
101
101
|
return_value
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
-
def
|
106
|
-
|
107
|
-
|
105
|
+
def _after_do_execute_before_callbacks(method, object, *args)
|
106
|
+
_after_do_each_callback_wrapped(:before, method) do |callback|
|
107
|
+
callback.call(*args, method, object)
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
-
def
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
111
|
+
def _after_do_execute_after_callbacks(method, object, *args, return_value)
|
112
|
+
_after_do_each_callback_wrapped(:after, method) do |callback|
|
113
|
+
callback.call(*args, method, return_value, object)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def _after_do_each_callback_wrapped(type, method)
|
118
|
+
@_after_do_callbacks[type][method].each do |block|
|
119
|
+
begin
|
120
|
+
yield block
|
121
|
+
rescue Exception => error
|
122
|
+
raise CallbackError, "#{error.class}: #{error.message} raised during an after_do callback block for method '#{method}' on the instance #{self.inspect} defined in the file #{block.source_location[0]} in line #{block.source_location[1]}.\n This is the backtrace of the original error: \n #{error.backtrace.join("\n")}"
|
123
|
+
end
|
116
124
|
end
|
117
125
|
end
|
118
126
|
end
|
data/lib/after_do/version.rb
CHANGED
data/samples/getting_a_hold.rb
CHANGED
@@ -2,32 +2,55 @@ require 'after_do'
|
|
2
2
|
|
3
3
|
class Example
|
4
4
|
def zero
|
5
|
-
|
5
|
+
0
|
6
6
|
end
|
7
7
|
|
8
8
|
def two(a, b)
|
9
|
-
|
9
|
+
a + b
|
10
10
|
end
|
11
11
|
|
12
12
|
def value
|
13
|
-
'
|
13
|
+
'some_value'
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
Example.extend AfterDo
|
18
18
|
|
19
19
|
Example.after :zero do puts 'Hello!' end
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
Example.after :
|
20
|
+
|
21
|
+
# If the callback method takes no arguments then the first argument passed to
|
22
|
+
# the block with will the method name, then the return value and finally the object itself
|
23
|
+
Example.after :zero do |_name, _return, obj| puts obj.value end
|
24
|
+
|
25
|
+
# The method arguments are passed to the callback as well:
|
26
|
+
Example.after :two do |first, second| puts "#{first} #{second}" end
|
27
|
+
|
28
|
+
# Now alltogether:
|
29
|
+
Example.after :two do |a, b, name, value, obj|
|
30
|
+
puts "after: #{a} #{b} #{name} #{value} #{obj.value}"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Remember before can't get a return value as at that point the method hasn't
|
34
|
+
# beene executed:
|
35
|
+
Example.before :two do |a, b, name, obj|
|
36
|
+
puts "before: #{a} #{b} #{name} #{obj.value}"
|
37
|
+
end
|
38
|
+
|
39
|
+
# You can also use the * to soak up all the method arguments and get
|
40
|
+
# straight to the instance:
|
41
|
+
Example.after :two do |*args, _name, _return, obj|
|
42
|
+
puts "args passed to callback: #{args.join(', ')}"
|
43
|
+
puts 'just ' + obj.value
|
44
|
+
end
|
24
45
|
|
25
46
|
e = Example.new
|
26
47
|
e.zero
|
27
|
-
e.two
|
48
|
+
e.two 1, 2
|
28
49
|
# prints:
|
29
50
|
# Hello!
|
30
51
|
# some value
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
52
|
+
# before: 1 2 two some_value
|
53
|
+
# 1 2
|
54
|
+
# after: 1 2 two 3 some_value
|
55
|
+
# args passed to callback: 1, 2
|
56
|
+
# just some value
|
data/samples/singleton.rb
CHANGED
data/samples/with_module.rb
CHANGED
@@ -17,7 +17,15 @@ class C
|
|
17
17
|
include M
|
18
18
|
|
19
19
|
def method
|
20
|
-
puts '
|
20
|
+
puts 'Overridden method'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class D
|
25
|
+
prepend M
|
26
|
+
|
27
|
+
def method
|
28
|
+
puts 'Wanna be Overriden method'
|
21
29
|
end
|
22
30
|
end
|
23
31
|
|
@@ -26,9 +34,15 @@ M.after :method do puts 'method called' end
|
|
26
34
|
|
27
35
|
A.new.method
|
28
36
|
B.new.method
|
29
|
-
|
37
|
+
|
38
|
+
# won't call callback since the implementation was overriden
|
39
|
+
C.new.method
|
40
|
+
|
41
|
+
# will call callback since the module extending AfterDo was prepended
|
42
|
+
D.new.method
|
30
43
|
|
31
44
|
# Output is:
|
32
45
|
# method called
|
33
46
|
# method called
|
34
|
-
# Overridden method
|
47
|
+
# Overridden method
|
48
|
+
# method called
|
data/spec/after_do_spec.rb
CHANGED
@@ -21,7 +21,7 @@ describe AfterDo do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def two(param1, param2)
|
24
|
-
param2
|
24
|
+
param1 + param2
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -117,7 +117,7 @@ describe AfterDo do
|
|
117
117
|
end
|
118
118
|
|
119
119
|
before :each do
|
120
|
-
@dummy_class.send callback_adder,
|
120
|
+
@dummy_class.send callback_adder, :zero do raise StandardError, 'silly message' end
|
121
121
|
end
|
122
122
|
|
123
123
|
it 'raises a CallbackError' do
|
@@ -149,12 +149,12 @@ describe AfterDo do
|
|
149
149
|
end
|
150
150
|
|
151
151
|
it 'can handle methods with a parameter' do
|
152
|
-
@dummy_class.send callback_adder,
|
152
|
+
@dummy_class.send callback_adder, :one do mockie.call_method end
|
153
153
|
dummy_instance.one 5
|
154
154
|
end
|
155
155
|
|
156
156
|
it 'can handle methods with 2 parameters' do
|
157
|
-
@dummy_class.send callback_adder,
|
157
|
+
@dummy_class.send callback_adder, :two do mockie.call_method end
|
158
158
|
dummy_instance.two 5, 8
|
159
159
|
end
|
160
160
|
end
|
@@ -162,13 +162,13 @@ describe AfterDo do
|
|
162
162
|
describe 'with parameters for the given block' do
|
163
163
|
it 'can handle one block parameter' do
|
164
164
|
expect(mockie).to receive(:call_method).with(5)
|
165
|
-
@dummy_class.send callback_adder,
|
165
|
+
@dummy_class.send callback_adder, :one do |i| mockie.call_method i end
|
166
166
|
dummy_instance.one 5
|
167
167
|
end
|
168
168
|
|
169
169
|
it 'can handle two block parameters' do
|
170
170
|
expect(mockie).to receive(:call_method).with(5, 8)
|
171
|
-
@dummy_class.send callback_adder,
|
171
|
+
@dummy_class.send callback_adder, :two do |i, j| mockie.call_method i, j end
|
172
172
|
dummy_instance.two 5, 8
|
173
173
|
end
|
174
174
|
end
|
@@ -182,7 +182,7 @@ describe AfterDo do
|
|
182
182
|
|
183
183
|
it 'can take multiple method names as arguments' do
|
184
184
|
expect(mockie).to receive(:call_method).exactly(3).times
|
185
|
-
@dummy_class.send callback_adder,
|
185
|
+
@dummy_class.send callback_adder, :zero, :one, :two do
|
186
186
|
mockie.call_method
|
187
187
|
end
|
188
188
|
call_all_3_methods
|
@@ -190,7 +190,7 @@ describe AfterDo do
|
|
190
190
|
|
191
191
|
it 'can get the methods as an Array' do
|
192
192
|
expect(mockie).to receive(:call_method).exactly(3).times
|
193
|
-
@dummy_class.send callback_adder,
|
193
|
+
@dummy_class.send callback_adder, [:zero, :one, :two] do
|
194
194
|
mockie.call_method
|
195
195
|
end
|
196
196
|
call_all_3_methods
|
@@ -208,7 +208,7 @@ describe AfterDo do
|
|
208
208
|
describe 'it can get a hold of self, if needbe' do
|
209
209
|
it 'works for a method without arguments' do
|
210
210
|
expect(mockie).to receive(:call_method).with(dummy_instance)
|
211
|
-
@dummy_class.send callback_adder,
|
211
|
+
@dummy_class.send callback_adder, :zero do |*_things, object|
|
212
212
|
mockie.call_method(object)
|
213
213
|
end
|
214
214
|
dummy_instance.zero
|
@@ -216,13 +216,39 @@ describe AfterDo do
|
|
216
216
|
|
217
217
|
it 'works for a method with 2 arguments' do
|
218
218
|
expect(mockie).to receive(:call_method).with(1, 2, dummy_instance)
|
219
|
-
@dummy_class.send callback_adder, :two do |first, second, object|
|
219
|
+
@dummy_class.send callback_adder, :two do |first, second, *_things, object|
|
220
220
|
mockie.call_method(first, second, object)
|
221
221
|
end
|
222
222
|
dummy_instance.two 1, 2
|
223
223
|
end
|
224
224
|
end
|
225
225
|
|
226
|
+
describe 'it can get a hold of the method name, if needbe' do
|
227
|
+
it 'works for a method without arguments' do
|
228
|
+
expect(mockie).to receive(:call_method).with(:zero)
|
229
|
+
@dummy_class.send callback_adder, :zero do |method_name|
|
230
|
+
mockie.call_method(method_name)
|
231
|
+
end
|
232
|
+
dummy_instance.zero
|
233
|
+
end
|
234
|
+
|
235
|
+
it 'works for a method with arguments' do
|
236
|
+
expect(mockie).to receive(:call_method).with(1, 2, :two)
|
237
|
+
@dummy_class.send callback_adder, :two do |arg1, arg2, method_name|
|
238
|
+
mockie.call_method(arg1, arg2, method_name)
|
239
|
+
end
|
240
|
+
dummy_instance.two(1, 2)
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'can also pass along the object' do
|
244
|
+
expect(mockie).to receive(:call_method).with(1, 2, :two, dummy_instance)
|
245
|
+
@dummy_class.send callback_adder, :two do |arg1, arg2, method_name, *, inst|
|
246
|
+
mockie.call_method(arg1, arg2, method_name, inst)
|
247
|
+
end
|
248
|
+
dummy_instance.two(1, 2)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
226
252
|
describe 'inheritance' do
|
227
253
|
let(:inherited_instance) {@inherited_class.new}
|
228
254
|
|
@@ -414,4 +440,22 @@ describe AfterDo do
|
|
414
440
|
dummy_instance.zero
|
415
441
|
end
|
416
442
|
end
|
443
|
+
|
444
|
+
describe 'difference between before and after' do
|
445
|
+
it 'after also has access to the return value' do
|
446
|
+
expect(mockie).to receive(:call).with(10, 20, dummy_instance, :two, 30)
|
447
|
+
@dummy_class.after :two do |*args, name, value, inst|
|
448
|
+
mockie.call(args.first, args.last, inst, name, value)
|
449
|
+
end
|
450
|
+
dummy_instance.two 10, 20
|
451
|
+
end
|
452
|
+
|
453
|
+
it 'before can not access to the return value' do
|
454
|
+
expect(mockie).to receive(:call).with(10, 20, dummy_instance, :two)
|
455
|
+
@dummy_class.before :two do |*args, name, inst|
|
456
|
+
mockie.call(args.first, args.last, inst, name)
|
457
|
+
end
|
458
|
+
dummy_instance.two 10, 20
|
459
|
+
end
|
460
|
+
end
|
417
461
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
require 'simplecov'
|
2
2
|
require 'coveralls'
|
3
|
-
SimpleCov.
|
4
|
-
|
5
|
-
SimpleCov::Formatter::HTMLFormatter
|
6
|
-
]
|
3
|
+
SimpleCov.formatters = [Coveralls::SimpleCov::Formatter,
|
4
|
+
SimpleCov::Formatter::HTMLFormatter]
|
7
5
|
|
8
6
|
SimpleCov.start do
|
9
7
|
add_filter '/spec/'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: after_do
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Pfeiffer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: after_do is a gem that let's you execute a block of your choice after
|
14
14
|
or before a specific method is called on a class. This is inspired by Aspect Oriented
|
@@ -23,6 +23,7 @@ files:
|
|
23
23
|
- ".ruby-gemset"
|
24
24
|
- ".ruby-version"
|
25
25
|
- ".travis.yml"
|
26
|
+
- CHANGELOG.md
|
26
27
|
- Gemfile
|
27
28
|
- Guardfile
|
28
29
|
- LICENSE.txt
|
@@ -61,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
62
|
version: '0'
|
62
63
|
requirements: []
|
63
64
|
rubyforge_project:
|
64
|
-
rubygems_version: 2.
|
65
|
+
rubygems_version: 2.4.8
|
65
66
|
signing_key:
|
66
67
|
specification_version: 4
|
67
68
|
summary: after_do allows you to add simple after/before hooks to methods
|