wrap 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/wrap.rb +33 -11
  2. data/test/wrap_test.rb +13 -1
  3. data/wrap.gemspec +1 -1
  4. metadata +3 -3
data/lib/wrap.rb CHANGED
@@ -5,7 +5,7 @@
5
5
  ##
6
6
  #
7
7
  class << Wrap
8
- Wrap::Version = '1.3.0' unless defined?(Wrap::Version)
8
+ Wrap::Version = '1.4.0' unless defined?(Wrap::Version)
9
9
 
10
10
  def version
11
11
  Wrap::Version
@@ -41,19 +41,40 @@
41
41
  other.send(:class_eval, &InstanceMethods)
42
42
  end
43
43
 
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
44
+ def Wrap.code_for(method)
45
+ name = method.name.to_s
46
+ arity = method.arity
47
+
48
+ case
49
+ when arity == 0
50
+ signature = <<-__.strip
51
+ def #{ name }(&block)
52
+ args = []
53
+ __
54
+
55
+ when arity < 0
56
+ argv = Array.new(arity.abs - 1){|i| "arg#{ i }"}
57
+ argv.push('*args')
58
+ argv = argv.join(', ')
59
+
60
+ signature = <<-__.strip
61
+ def #{ name }(#{ argv }, &block)
62
+ args = [#{ argv }]
63
+ __
64
+
65
+ when arity > 0
66
+ argv = Array.new(arity){|i| "arg#{ i }"}
67
+ argv = argv.join(', ')
68
+
69
+ signature = <<-__.strip
70
+ def #{ name }(#{ argv }, &block)
71
+ args = [#{ argv }]
72
+ __
51
73
  end
52
74
 
53
75
  code =
54
76
  <<-__
55
- def #{ name }(#{ argv }, &block)
56
- args = #{ argv_array }
77
+ #{ signature.strip }
57
78
 
58
79
  if running_callbacks?(#{ name.inspect })
59
80
  return wrapped_#{ name }(*args, &block)
@@ -128,6 +149,7 @@
128
149
  def wrap!(name)
129
150
  name = name.to_s
130
151
  method = instance_method(name)
152
+ arity = method.arity
131
153
 
132
154
  wrapping! name do
133
155
  name = name.to_s
@@ -141,7 +163,7 @@
141
163
 
142
164
  alias_method(wrapped_name, name)
143
165
 
144
- module_eval(Wrap.code_for(name, method.arity))
166
+ module_eval(Wrap.code_for(method))
145
167
  end
146
168
  end
147
169
 
data/test/wrap_test.rb CHANGED
@@ -329,7 +329,7 @@ Testing Wrap do
329
329
 
330
330
  ##
331
331
  #
332
- testing 'that wrapping preserves method arity' do
332
+ testing 'that wrapping preserves method arity like a boss' do
333
333
  assert do
334
334
  wrapped_class do
335
335
  def foo(x, y) [x, y] end
@@ -341,6 +341,18 @@ Testing Wrap do
341
341
 
342
342
  assert{ instance_method(:foo).arity == 2 }
343
343
  assert{ new.foo(4, 2) == [4, 2] }
344
+
345
+ def foo() end
346
+ assert{ instance_method(:foo).arity == 0 }
347
+
348
+ def foo(x) end
349
+ assert{ instance_method(:foo).arity == 1 }
350
+
351
+ def foo(*x) end
352
+ assert{ instance_method(:foo).arity == -1 }
353
+
354
+ def foo(x, *y) end
355
+ assert{ instance_method(:foo).arity == -2 }
344
356
  end
345
357
  end
346
358
  end
data/wrap.gemspec CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "wrap"
6
- spec.version = "1.3.0"
6
+ spec.version = "1.4.0"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "wrap"
9
9
  spec.description = "description: wrap kicks the ass"
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.3.0
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: map
16
- requirement: &70321392671840 !ruby/object:Gem::Requirement
16
+ requirement: &70355214698460 !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: *70321392671840
24
+ version_requirements: *70355214698460
25
25
  description: ! 'description: wrap kicks the ass'
26
26
  email: ara.t.howard@gmail.com
27
27
  executables: []