proxeze 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -121,14 +121,16 @@ The special case to this is that class methods defined in Object are not proxied
121
121
  Note: I don't recommend overriding the #hash method on your class, this serves only as an example.
122
122
 
123
123
  == Method Interceptions
124
- Proxeze has the ability to surround instance method calls with _before_ and _after_ callbacks. This support was lifted straight from proxy_machine (https://github.com/tulios/proxy_machine).
124
+ Proxeze has the ability to surround instance method calls with _before_ and _after_ callbacks. This support was lifted straight from proxy_machine (https://github.com/tulios/proxy_machine). Unlike proxy_machine, all callbacks receive the same set of parameters:
125
+ a reference to the object, the arguments passed, the symbol of the called method, and the result of execution (this result could be nil,
126
+ and necessarily *will* be nil in _before_ and _before_all_ interceptions).
125
127
 
126
128
  === Defining callbacks at the method level
127
129
 
128
130
  ==== before
129
131
 
130
132
  p = Proxeze.for [0, 1, 2, 3] do
131
- before :reverse do |obj, args|
133
+ before :reverse do |obj, args, mid, result|
132
134
  obj << obj.length
133
135
  end
134
136
  end
@@ -138,24 +140,20 @@ Proxeze has the ability to surround instance method calls with _before_ and _aft
138
140
  ==== after
139
141
 
140
142
  p = Proxeze.for [4, 2, 3] do
141
- after :reverse do |obj, result, args|
143
+ after :reverse do |obj, args, mid, result|
142
144
  result.sort
143
145
  end
144
146
  end
145
147
 
146
- p.reverse => [4, 3, 2] # We reordered the list
147
-
148
- You will always receive the arguments passed to the original method.
148
+ p.reverse => [4, 3, 2] # We reordered the list
149
149
 
150
150
  === Defining callbacks for all method calls
151
151
 
152
152
  ==== before_all
153
- This callback will receive a reference of the object, the symbol of the called method and the
154
- original arguments passed.
155
153
 
156
154
  logged = nil
157
155
  p = Proxeze.for [0, 1, 2, 3] do
158
- before_all do |obj, method, args|
156
+ before_all do |obj, args, method, result|
159
157
  logged = "before #{method} on #{obj.inspect} with args[#{args.inspect}]"
160
158
  end
161
159
  end
@@ -169,12 +167,10 @@ original arguments passed.
169
167
  logged # => before unshift on [0, 1, 2, 3] with args[[9]]
170
168
 
171
169
  ==== after_all
172
- This callback will receive a reference of the object, the result of execution (this result could be nil),
173
- the symbol of the called method and the arguments passed.
174
170
 
175
171
  logged = nil
176
172
  p = Proxeze.for [1, 2, 3] do
177
- after_all do |obj, result, method, args|
173
+ after_all do |obj, args, method, result|
178
174
  logged = "after #{method} on #{obj.inspect} with args[#{args.inspect}], result is now [#{result}]"
179
175
  result
180
176
  end
@@ -187,14 +183,14 @@ the symbol of the called method and the arguments passed.
187
183
 
188
184
  === Registering a class to perform a callback
189
185
 
190
- The constructor will receive the object, in case of an :after it will receive the result too.
191
- You need to have a :call method. Proxeze will create a new instance of the class every time it
192
- needs to use it. You could use this feature with the before_all and after_all too.
186
+ The initializer will receive an extra parameter, the type of the callback (one of #before, #before_all, #after, or #after_all).
187
+ In addition it also receives the object, the arguments passed to the method, the method called, and a result (which is populated for #after and
188
+ #after_all hooks). You will also need to define a #call method. Proxeze will create a new instance of the class every time it needs to use it.
193
189
 
194
190
  # Example of class
195
191
  class SortPerformer
196
- def initialize object, result = nil, method = nil, args = nil
197
- @object = object; @result = result; @method = method, @args = args
192
+ def initialize callback_type, object, args = nil, method = nil, result = nil
193
+ @object = object; @args = args; @method = method; @result = result
198
194
  end
199
195
 
200
196
  def call; @object.sort! end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 1.2.0
@@ -14,8 +14,8 @@ module Proxeze
14
14
  # The after_all hook will receive 4 arguments:
15
15
  # target, result, method id, and any arguments
16
16
  # to the method that was called.
17
- def after_all &blk
18
- insert_into_callback_chain :hook => :after_all, :blk => blk
17
+ def after_all *args, &blk
18
+ insert_into_callback_chain :hook => :after_all, :blk => blk, :args => args
19
19
  end
20
20
 
21
21
  # The before hook will receive 2 arguments:
@@ -28,8 +28,8 @@ module Proxeze
28
28
  # The before_all hook will receive 3 arguments:
29
29
  # target, method id, and any arguments to the
30
30
  # method that is being called.
31
- def before_all &blk
32
- insert_into_callback_chain :hook => :before_all, :blk => blk
31
+ def before_all *args, &blk
32
+ insert_into_callback_chain :hook => :before_all, :blk => blk, :args => args
33
33
  end
34
34
 
35
35
  private
@@ -12,12 +12,12 @@ def Delegator.delegating_block mid
12
12
  before_all = self.class.hooks[:before_all]
13
13
  before = self.class.hooks[:before] ? self.class.hooks[:before][mid] : nil
14
14
 
15
- execute_call(before_all, target, mid, args)
16
- execute_call(before, target, args)
15
+ execute_call(before_all, :before_all, target, args, mid)
16
+ execute_call(before, :before, target, args, mid)
17
17
 
18
18
  result = target.__send__(mid, *args, &block)
19
- result_after = execute_call(after, target, result, args)
20
- result_after_all = execute_call(after_all, target, result, mid, args)
19
+ result_after = execute_call(after, :after, target, args, mid, result)
20
+ result_after_all = execute_call(after_all, :after_all, target, args, mid, result)
21
21
  return result_after_all if result_after_all
22
22
  return result_after if result_after
23
23
  result
@@ -19,16 +19,16 @@ module Proxeze
19
19
  self.class.after mid, *args, &blk
20
20
  end
21
21
 
22
- def after_all &blk
23
- self.class.after_all &blk
22
+ def after_all *args, &blk
23
+ self.class.after_all *args, &blk
24
24
  end
25
25
 
26
26
  def before mid, *args, &blk
27
27
  self.class.before mid, *args, &blk
28
28
  end
29
29
 
30
- def before_all &blk
31
- self.class.before_all &blk
30
+ def before_all *args, &blk
31
+ self.class.before_all *args, &blk
32
32
  end
33
33
 
34
34
  private
@@ -37,13 +37,13 @@ module Proxeze
37
37
  result = nil
38
38
  if executor.respond_to? :each
39
39
  executor.each do |e|
40
- result = e.send :call, *args if proc?(e)
40
+ result = e.send :call, *args[1..-1] if proc?(e)
41
41
  result = e.send(:new, *args).call if class?(e)
42
42
  end
43
43
  return result
44
44
  end
45
45
 
46
- return executor.send :call, *args if proc?(executor)
46
+ return executor.send :call, *args[1..-1] if proc?(executor)
47
47
  return executor.send(:new, *args).call if class?(executor)
48
48
  end
49
49
 
data/proxeze.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{proxeze}
8
- s.version = "1.1.1"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jason Rogers"]
data/spec/proxeze_spec.rb CHANGED
@@ -177,8 +177,8 @@ describe Proxeze do
177
177
  it "should run 'before' callbacks" do
178
178
  baz_method_args = nil
179
179
  Proxeze.proxy( ClassWhereinWeMeetBeforeBlocks ) do
180
- before :baz do |*args|
181
- baz_method_args = args.last
180
+ before :baz do |target, args, mid, result|
181
+ baz_method_args = args
182
182
  end
183
183
  end
184
184
  instance = ClassWhereinWeMeetBeforeBlocks.new
@@ -191,9 +191,8 @@ describe Proxeze do
191
191
  it "should run 'before_all' callbacks" do
192
192
  callbacks = {}
193
193
  Proxeze.proxy( ClassWhereinWeMeetBeforeBlocks ) do
194
- before_all do |*args|
195
- target, mid, arguments = *args
196
- callbacks[mid] = arguments
194
+ before_all do |target, args, mid, result|
195
+ callbacks[mid] = args
197
196
  end
198
197
  end
199
198
  instance = ClassWhereinWeMeetBeforeBlocks.new
@@ -210,9 +209,9 @@ describe Proxeze do
210
209
  it "should run 'after' callbacks" do
211
210
  callbacks = {}
212
211
  Proxeze.proxy( ClassWhereinWeMeetBeforeBlocks ) do
213
- after :foo do |*args|
214
- callbacks[:foo] = args.last
215
- args[-2]
212
+ after :foo do |target, args, mid, result|
213
+ callbacks[mid] = args
214
+ result
216
215
  end
217
216
  end
218
217
  instance = ClassWhereinWeMeetBeforeBlocks.new
@@ -227,8 +226,7 @@ describe Proxeze do
227
226
  it "should run 'after_all' callbacks" do
228
227
  callbacks = {}
229
228
  Proxeze.proxy( ClassWhereinWeMeetBeforeBlocks ) do
230
- after_all do |*args|
231
- target, result, mid, arguments = *args
229
+ after_all do |target, arguments, mid, result|
232
230
  callbacks[mid] = result
233
231
  result * 2
234
232
  end
@@ -246,14 +244,13 @@ describe Proxeze do
246
244
 
247
245
  it "should be able to add hooks to a proxied instance" do
248
246
  a = Proxeze.for [0, 1, 3, 2, 5, 4] do
249
- before :reverse do |target, mid, arguments|
247
+ before :reverse do |target, arguments, mid|
250
248
  target << target.length
251
249
  end
252
250
  end
253
251
  a.reverse.should == [6, 4, 5, 2, 3, 1, 0]
254
252
 
255
- a.after :sort do |*args|
256
- target, result, arguments = *args
253
+ a.after :sort do |target, arguments, mid, result|
257
254
  result << result.length
258
255
  end
259
256
  a.sort.should == [0, 1, 2, 3, 4, 5, 6, 7]
@@ -267,7 +264,7 @@ describe Proxeze do
267
264
 
268
265
  it "should accept a class for the callbacks" do
269
266
  class SortPerformer
270
- def initialize object, result = nil, method = nil, args = nil
267
+ def initialize callback_type, object, args = nil, method = nil, result = nil
271
268
  @object = object; @result = result; @method = method, @args = args
272
269
  end
273
270
 
@@ -278,4 +275,46 @@ describe Proxeze do
278
275
  end
279
276
  p.reverse.should == [1, 2, 3]
280
277
  end
278
+
279
+ it "should accept a class for before_all and after_all callbacks" do
280
+ class MyClass
281
+ attr_accessor :foo
282
+ def inspect
283
+ 'an instance of MyClass'
284
+ end
285
+ end
286
+
287
+ class DebugLogInterceptor
288
+ def self.logger
289
+ @logger ||= []
290
+ @logger
291
+ end
292
+
293
+ def initialize callback_type, object, args = nil, method = nil, result = nil
294
+ @callback_type = callback_type
295
+ @object = object
296
+ @result = result
297
+ @method = method
298
+ @args = args
299
+ end
300
+
301
+ def call
302
+ self.class.logger << log_message
303
+ end
304
+
305
+ def log_message
306
+ "#{@callback_type}: #{@method} on #{@object.inspect} with args[#{@args.inspect}], result is [#{@result}]"
307
+ end
308
+ end
309
+
310
+ Proxeze.proxy MyClass do
311
+ after_all DebugLogInterceptor
312
+ before_all DebugLogInterceptor
313
+ end
314
+
315
+ o = MyClass.new
316
+ o.foo = 2
317
+ DebugLogInterceptor.logger.should == ["before_all: foo= on an instance of MyClass with args[[2]], result is []",
318
+ "after_all: foo= on an instance of MyClass with args[[2]], result is [2]"]
319
+ end
281
320
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: proxeze
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.1
5
+ version: 1.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jason Rogers
@@ -192,7 +192,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
192
  requirements:
193
193
  - - ">="
194
194
  - !ruby/object:Gem::Version
195
- hash: -2385236588266356416
195
+ hash: 1901228362628821152
196
196
  segments:
197
197
  - 0
198
198
  version: "0"