wrapit 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wrapit.rb CHANGED
@@ -3,3 +3,7 @@ require 'wrapped'
3
3
  require "wrapit/version"
4
4
  require "wrapit/attr_wrappable"
5
5
  require "wrapit/method_wrappable"
6
+
7
+ module Wrapit
8
+ class InvalidCallerError < StandardError; end
9
+ end
@@ -7,13 +7,22 @@ module Wrapit::AttrWrappable
7
7
  attr_accessor *args.map { |m| "#{m.to_s}_naked".to_sym }
8
8
 
9
9
  args.each do |method|
10
- define_method method do
11
- send(:"#{method}_naked").wrapped
12
- end
10
+ aw_create_naked_method(method)
11
+ aw_create_wrapped_method(method)
12
+ end
13
+ end
14
+
15
+ def aw_create_naked_method(method)
16
+ raise Wrapit::InvalidCallerError unless caller[0] =~ /attr_wrappable/
17
+ define_method method do
18
+ send(:"#{method}_naked").wrapped
19
+ end
20
+ end
13
21
 
14
- define_method :"#{method}=" do |value|
15
- send(:"#{method}_naked=", value)
16
- end
22
+ def aw_create_wrapped_method(method)
23
+ raise Wrapit::InvalidCallerError unless caller[0] =~ /attr_wrappable/
24
+ define_method :"#{method}=" do |value|
25
+ send(:"#{method}_naked=", value)
17
26
  end
18
27
  end
19
28
  end
@@ -4,15 +4,37 @@ module Wrapit::MethodWrappable
4
4
  private
5
5
 
6
6
  def method_wrappable(*args)
7
- class_eval do
8
- args.each do |method|
9
- define_method :"#{method}_naked" do
10
- self.class.superclass.instance_method(method).bind(self).call
11
- end
7
+ args.each do |method|
8
+ has_args = (instance_method(method).arity != 0)
9
+ mw_create_naked_method(method, has_args)
10
+ mw_create_wrapped_method(method, has_args)
11
+ end
12
+ end
13
+
14
+ def mw_create_naked_method(method, has_args)
15
+ raise Wrapit::InvalidCallerError unless caller[0] =~ /method_wrappable/
16
+
17
+ if has_args
18
+ define_method :"#{method}_naked" do |*args|
19
+ self.class.superclass.instance_method(method).bind(self).call(*args)
20
+ end
21
+ else
22
+ define_method :"#{method}_naked" do
23
+ self.class.superclass.instance_method(method).bind(self).call
24
+ end
25
+ end
26
+ end
12
27
 
13
- define_method method do
14
- super().wrapped
15
- end
28
+ def mw_create_wrapped_method(method, has_args)
29
+ raise Wrapit::InvalidCallerError unless caller[0] =~ /method_wrappable/
30
+
31
+ if has_args
32
+ define_method method do |*args|
33
+ super(*args).wrapped
34
+ end
35
+ else
36
+ define_method method do
37
+ super().wrapped
16
38
  end
17
39
  end
18
40
  end
@@ -1,3 +1,3 @@
1
1
  module Wrapit
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -49,6 +49,15 @@ describe Wrapit::AttrWrappable do
49
49
  destroy_class
50
50
  end
51
51
 
52
+ it "wrapped method should return correct value when unwrapped" do
53
+ build_class
54
+ FooBar.module_eval { attr_wrappable :test_method }
55
+ foo_bar = FooBar.new
56
+ foo_bar.test_method = "something"
57
+ expect(foo_bar.test_method.unwrap).to eq "something"
58
+ destroy_class
59
+ end
60
+
52
61
  def build_class
53
62
  define_class 'FooBar', 'Object' do |klass|
54
63
  klass.send :include, Wrapit::AttrWrappable
@@ -37,6 +37,14 @@ describe Wrapit::MethodWrappable do
37
37
  destroy_class
38
38
  end
39
39
 
40
+ it "wrapped method should return correct value when unwrapped" do
41
+ build_class
42
+ Object.module_eval { define_method :test_method do "something"; end }
43
+ FooBar.module_eval { method_wrappable :test_method }
44
+ expect(FooBar.new.test_method.unwrap).to eq "something"
45
+ destroy_class
46
+ end
47
+
40
48
  it "wrapped method should return same value as naked method" do
41
49
  build_class
42
50
  Object.module_eval { define_method :test_method do "something"; end }
@@ -46,6 +54,22 @@ describe Wrapit::MethodWrappable do
46
54
  destroy_class
47
55
  end
48
56
 
57
+ it "method_wrappable should work with methods that have arguments" do
58
+ build_class
59
+ Object.module_eval { define_method :test_method do |arg1, arg2| "#{arg1}, #{arg2}"; end }
60
+ FooBar.module_eval { method_wrappable :test_method }
61
+ expect(FooBar.new.test_method("hello", "world").unwrap).to eq "hello, world"
62
+ destroy_class
63
+ end
64
+
65
+ it "method_wrappable should work with methods that have splat arguments" do
66
+ build_class
67
+ Object.module_eval { define_method :test_method do |*args| args.join(", "); end }
68
+ FooBar.module_eval { method_wrappable :test_method }
69
+ expect(FooBar.new.test_method("hello", "world").unwrap).to eq "hello, world"
70
+ destroy_class
71
+ end
72
+
49
73
  def build_class
50
74
  define_class 'FooBar', 'Object' do |klass|
51
75
  klass.send :include, Wrapit::MethodWrappable
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrapit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-10 00:00:00.000000000 Z
12
+ date: 2014-06-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: wrapped
@@ -127,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
127
  version: '0'
128
128
  segments:
129
129
  - 0
130
- hash: -4453862613727087029
130
+ hash: -1557926693994722533
131
131
  required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  none: false
133
133
  requirements:
@@ -136,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  segments:
138
138
  - 0
139
- hash: -4453862613727087029
139
+ hash: -1557926693994722533
140
140
  requirements: []
141
141
  rubyforge_project:
142
142
  rubygems_version: 1.8.25