wrap 0.4.3 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/wrap.rb +30 -30
- data/test/wrap_test.rb +34 -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 = '0.
|
8
|
+
Version = '0.5.0' unless defined?(Version)
|
9
9
|
|
10
10
|
def version
|
11
11
|
Version
|
@@ -16,31 +16,13 @@
|
|
16
16
|
'map' => [ 'map' , ' >= 4.7.1' ]
|
17
17
|
}
|
18
18
|
end
|
19
|
-
|
20
|
-
def libdir(*args, &block)
|
21
|
-
@libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
|
22
|
-
args.empty? ? @libdir : File.join(@libdir, *args)
|
23
|
-
ensure
|
24
|
-
if block
|
25
|
-
begin
|
26
|
-
$LOAD_PATH.unshift(@libdir)
|
27
|
-
block.call()
|
28
|
-
ensure
|
29
|
-
$LOAD_PATH.shift()
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def load(*libs)
|
35
|
-
libs = libs.join(' ').scan(/[^\s+]+/)
|
36
|
-
Wrap.libdir{ libs.each{|lib| Kernel.load(lib) } }
|
37
|
-
end
|
38
19
|
end
|
39
20
|
|
40
21
|
##
|
41
22
|
#
|
42
23
|
begin
|
43
24
|
require 'rubygems'
|
25
|
+
Wrap.dependencies.each{|name, dependency| gem(*dependency)}
|
44
26
|
rescue LoadError
|
45
27
|
nil
|
46
28
|
end
|
@@ -82,15 +64,8 @@
|
|
82
64
|
end
|
83
65
|
|
84
66
|
ClassMethods = proc do
|
85
|
-
def wrap(name, *args, &block)
|
86
|
-
define_method(name){} unless method_defined?(name)
|
87
|
-
|
88
|
-
wrap!(name)
|
89
|
-
end
|
90
|
-
|
91
67
|
def method_added(name)
|
92
68
|
return super if wrapping?
|
93
|
-
|
94
69
|
begin
|
95
70
|
super
|
96
71
|
ensure
|
@@ -98,18 +73,43 @@
|
|
98
73
|
end
|
99
74
|
end
|
100
75
|
|
76
|
+
def include(other)
|
77
|
+
super
|
78
|
+
ensure
|
79
|
+
other.instance_methods.each do |name|
|
80
|
+
if wrapped?(name)
|
81
|
+
begin
|
82
|
+
remove_method(name)
|
83
|
+
rescue NameError
|
84
|
+
nil
|
85
|
+
end
|
86
|
+
rewrap!(name)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def wrap(name, *args, &block)
|
92
|
+
define_method(name){} unless method_defined?(name)
|
93
|
+
wrap!(name)
|
94
|
+
end
|
95
|
+
|
101
96
|
def wrapped?(name)
|
102
97
|
method_defined?("wrapped_#{ name }")
|
103
98
|
end
|
104
99
|
|
105
100
|
def wrap!(name)
|
101
|
+
name = name.to_s
|
102
|
+
method = instance_method(name)
|
103
|
+
|
106
104
|
wrapping! name do
|
107
105
|
name = name.to_s
|
108
106
|
wrapped_name = "wrapped_#{ name }"
|
109
107
|
|
110
|
-
|
111
|
-
|
112
|
-
|
108
|
+
begin
|
109
|
+
remove_method(wrapped_name)
|
110
|
+
rescue NameError
|
111
|
+
nil
|
112
|
+
end
|
113
113
|
|
114
114
|
alias_method(wrapped_name, name)
|
115
115
|
|
data/test/wrap_test.rb
CHANGED
@@ -207,6 +207,40 @@ Testing Wrap do
|
|
207
207
|
end
|
208
208
|
end
|
209
209
|
|
210
|
+
##
|
211
|
+
#
|
212
|
+
testing 'that methods added via module inclusion preserve wrapping too' do
|
213
|
+
c =
|
214
|
+
assert do
|
215
|
+
wrapped_class do
|
216
|
+
define_method(:foo){ accum.push(:original); accum }
|
217
|
+
|
218
|
+
wrap :foo
|
219
|
+
|
220
|
+
before(:foo){ accum.push(:before) }
|
221
|
+
after(:foo){ accum.push(:after) }
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
assert do
|
226
|
+
o = c.new
|
227
|
+
o.foo()
|
228
|
+
assert o.accum === [:before, :original, :after]
|
229
|
+
end
|
230
|
+
|
231
|
+
m =
|
232
|
+
Module.new do
|
233
|
+
define_method(:foo){ accum.push(:mixin); accum }
|
234
|
+
end
|
235
|
+
|
236
|
+
c.send(:include, m)
|
237
|
+
|
238
|
+
assert do
|
239
|
+
o = c.new
|
240
|
+
o.foo()
|
241
|
+
assert o.accum === [:before, :mixin, :after]
|
242
|
+
end
|
243
|
+
end
|
210
244
|
|
211
245
|
private
|
212
246
|
def wrapped_class(&block)
|
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: 0.
|
4
|
+
version: 0.5.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: 2011-
|
12
|
+
date: 2011-12-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: map
|
16
|
-
requirement: &
|
16
|
+
requirement: &70362008679240 !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: *70362008679240
|
25
25
|
description: ! 'description: wrap kicks the ass'
|
26
26
|
email: ara.t.howard@gmail.com
|
27
27
|
executables: []
|