wrap 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/wrap.rb +28 -17
- data/test/wrap_test.rb +18 -0
- data/wrap.gemspec +1 -1
- metadata +4 -4
data/lib/wrap.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
##
|
6
6
|
#
|
7
7
|
class << Wrap
|
8
|
-
Version = '1.
|
8
|
+
Version = '1.2.0' unless defined?(Version)
|
9
9
|
|
10
10
|
def version
|
11
11
|
Version
|
@@ -41,26 +41,37 @@
|
|
41
41
|
other.send(:class_eval, &InstanceMethods)
|
42
42
|
end
|
43
43
|
|
44
|
-
def Wrap.code_for(name)
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
44
|
+
def Wrap.code_for(name, arity)
|
45
|
+
if arity <= 0
|
46
|
+
argv = "*argv"
|
47
|
+
argv_array = "argv"
|
48
|
+
else
|
49
|
+
argv = Array.new(arity){|i| "arg#{ i }"}.join(', ')
|
50
|
+
argv_array = argv
|
51
|
+
end
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
53
|
+
code =
|
54
|
+
<<-__
|
55
|
+
def #{ name }(#{ argv }, &block)
|
56
|
+
args = #{ argv_array }
|
57
|
+
|
58
|
+
if running_callbacks?(#{ name.inspect })
|
59
|
+
return wrapped_#{ name }(*args, &block)
|
60
|
+
end
|
54
61
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
62
|
+
running_callbacks(#{ name.inspect }) do
|
63
|
+
catch(:halt) do
|
64
|
+
return false unless run_callbacks(:before, #{ name.inspect }, args)
|
65
|
+
|
66
|
+
begin
|
67
|
+
result = wrapped_#{ name }(*args, &block)
|
68
|
+
ensure
|
69
|
+
run_callbacks(:after, #{ name.inspect }, [result]) unless $!
|
70
|
+
end
|
59
71
|
end
|
60
72
|
end
|
61
73
|
end
|
62
|
-
|
63
|
-
__
|
74
|
+
__
|
64
75
|
end
|
65
76
|
|
66
77
|
ClassMethods = proc do
|
@@ -130,7 +141,7 @@
|
|
130
141
|
|
131
142
|
alias_method(wrapped_name, name)
|
132
143
|
|
133
|
-
module_eval(Wrap.code_for(name))
|
144
|
+
module_eval(Wrap.code_for(name, method.arity))
|
134
145
|
end
|
135
146
|
end
|
136
147
|
|
data/test/wrap_test.rb
CHANGED
@@ -327,6 +327,24 @@ Testing Wrap do
|
|
327
327
|
end
|
328
328
|
end
|
329
329
|
|
330
|
+
##
|
331
|
+
#
|
332
|
+
testing 'that wrapping preserves method arity' do
|
333
|
+
assert do
|
334
|
+
wrapped_class do
|
335
|
+
def foo(x, y) [x, y] end
|
336
|
+
|
337
|
+
assert{ instance_method(:foo).arity == 2 }
|
338
|
+
assert{ new.foo(4, 2) == [4, 2] }
|
339
|
+
|
340
|
+
wrap :foo
|
341
|
+
|
342
|
+
assert{ instance_method(:foo).arity == 2 }
|
343
|
+
assert{ new.foo(4, 2) == [4, 2] }
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
330
348
|
private
|
331
349
|
def wrapped_class(&block)
|
332
350
|
tc = self
|
data/wrap.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wrap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-02-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: map
|
16
|
-
requirement: &
|
16
|
+
requirement: &70251455330920 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 4.7.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70251455330920
|
25
25
|
description: ! 'description: wrap kicks the ass'
|
26
26
|
email: ara.t.howard@gmail.com
|
27
27
|
executables: []
|