forwardablex 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +8 -0
- data/Gemfile +3 -0
- data/HISTORY.md +4 -0
- data/README.md +16 -2
- data/Rakefile +2 -0
- data/forwardablex.gemspec +3 -2
- data/lib/forwardablex.rb +91 -24
- data/lib/forwardablex/version.rb +1 -1
- data/test/spec_forwardablex.rb +45 -1
- metadata +24 -7
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
ForwardableX is a Ruby library to extend forwardable.rb.
|
4
4
|
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/forwardablex.png)](http://badge.fury.io/rb/forwardablex) [![Build Status](https://travis-ci.org/keita/forwardablex.png?branch=master)](https://travis-ci.org/keita/forwardablex) [![Coverage Status](https://coveralls.io/repos/keita/forwardablex/badge.png?branch=master)](https://coveralls.io/r/keita/forwardablex) [![Code Climate](https://codeclimate.com/github/keita/forwardablex.png)](https://codeclimate.com/github/keita/forwardablex)
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
gem install forwardablex
|
@@ -43,7 +45,6 @@ Forwarder.new.m #=> "forwarded"
|
|
43
45
|
### Class Method Accessor
|
44
46
|
|
45
47
|
```ruby
|
46
|
-
Receiver = Struct(:name)
|
47
48
|
class Forwarder
|
48
49
|
class << self
|
49
50
|
def m
|
@@ -65,9 +66,22 @@ class Forwarder
|
|
65
66
|
@table = {:key => "forwarded"}
|
66
67
|
end
|
67
68
|
end
|
68
|
-
Forwarder.new.key
|
69
|
+
Forwarder.new.key #=> "forwarded"
|
69
70
|
```
|
70
71
|
|
72
|
+
### Identity
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class Forwarder
|
76
|
+
foward :identity, :self
|
77
|
+
end
|
78
|
+
Forwarder.new {|x| x == x.self} #=> true
|
79
|
+
```
|
80
|
+
|
81
|
+
## Documentation
|
82
|
+
|
83
|
+
- [API Documentation](http://rubydoc.info/gems/forwardablex)
|
84
|
+
|
71
85
|
## Licence
|
72
86
|
|
73
87
|
ForwardableX is free software distributed under MIT licence.
|
data/Rakefile
CHANGED
data/forwardablex.gemspec
CHANGED
@@ -17,7 +17,8 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ["lib"]
|
20
|
+
gem.add_development_dependency "rake"
|
20
21
|
gem.add_development_dependency "bacon"
|
21
|
-
gem.add_development_dependency "yard"
|
22
|
-
gem.add_development_dependency "redcarpet"
|
22
|
+
gem.add_development_dependency "yard"
|
23
|
+
gem.add_development_dependency "redcarpet" unless RUBY_PLATFORM == 'java'
|
23
24
|
end
|
data/lib/forwardablex.rb
CHANGED
@@ -57,21 +57,15 @@ module ForwardableX
|
|
57
57
|
context.instance_eval do
|
58
58
|
case receiver
|
59
59
|
when :class
|
60
|
-
|
61
|
-
|
62
|
-
|
60
|
+
forward_class_receiver(method, name)
|
61
|
+
when :identity
|
62
|
+
forward_identity_receiver(name)
|
63
63
|
when Symbol, String
|
64
|
-
|
65
|
-
instance_variable_get(receiver).__send__(method, *args, &b)
|
66
|
-
end
|
64
|
+
forward_named_receiver(receiver, method, name)
|
67
65
|
when Proc
|
68
|
-
|
69
|
-
instance_eval(&receiver).__send__(method, *args, &b)
|
70
|
-
end
|
66
|
+
forward_proc_receiver(receiver, method, name)
|
71
67
|
else
|
72
|
-
|
73
|
-
receiver.__send__(method, *args, &b)
|
74
|
-
end
|
68
|
+
forward_object_receiver(receiver, method, name)
|
75
69
|
end
|
76
70
|
end
|
77
71
|
end
|
@@ -122,21 +116,15 @@ module ForwardableX
|
|
122
116
|
context.instance_eval do
|
123
117
|
case receiver
|
124
118
|
when :class
|
125
|
-
|
126
|
-
|
127
|
-
|
119
|
+
forward_class_receiver(:[], name, key)
|
120
|
+
when :identity
|
121
|
+
forward_identity_receiver(name)
|
128
122
|
when Symbol, String
|
129
|
-
|
130
|
-
instance_variable_get(receiver).__send__(:[], key, *args, &b)
|
131
|
-
end
|
123
|
+
forward_named_receiver(receiver, :[], name, key)
|
132
124
|
when Proc
|
133
|
-
|
134
|
-
instance_eval(&receiver).__send__(:[], key, *args, &b)
|
135
|
-
end
|
125
|
+
forward_proc_receiver(receiver, :[], name, key)
|
136
126
|
else
|
137
|
-
|
138
|
-
receiver.__send__(:[], key, *args, &b)
|
139
|
-
end
|
127
|
+
forward_object_receiver(receiver, :[], name, key)
|
140
128
|
end
|
141
129
|
end
|
142
130
|
end
|
@@ -151,6 +139,85 @@ module ForwardableX
|
|
151
139
|
def forward_as_key!(receiver, *keys)
|
152
140
|
keys.each {|key| forward_as_key(receiver, key)}
|
153
141
|
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
# Forward the name to class receiver.
|
146
|
+
#
|
147
|
+
# @param method [Symbol]
|
148
|
+
# method name of the class
|
149
|
+
# @param name [Symbol]
|
150
|
+
# forwarder method name
|
151
|
+
# @param _args [Array<Object>]
|
152
|
+
# additional arguments
|
153
|
+
# @return [void]
|
154
|
+
def forward_class_receiver(method, name, *_args)
|
155
|
+
define_method(name) do |*args, &b|
|
156
|
+
self.class.__send__(method, *_args, *args, &b)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
# Forward the name to the named receiver.
|
161
|
+
#
|
162
|
+
# @param receiver [Symbol]
|
163
|
+
# receiver's name
|
164
|
+
# @param method [Symbol]
|
165
|
+
# method name of the receiver
|
166
|
+
# @param name [Symbol]
|
167
|
+
# forwarder method name
|
168
|
+
# @param _args [Array<Object>]
|
169
|
+
# additional arguments
|
170
|
+
# @return [void]
|
171
|
+
def forward_named_receiver(receiver, method, name, *_args)
|
172
|
+
define_method(name) do |*args, &b|
|
173
|
+
instance_variable_get(receiver).__send__(method, *_args, *args, &b)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
# Forward the name to the Proc result.
|
178
|
+
#
|
179
|
+
# @param proc [Proc]
|
180
|
+
# Proc object that generate the receiver
|
181
|
+
# @param method [Symbol]
|
182
|
+
# method name of the proc result
|
183
|
+
# @param name [Symbol]
|
184
|
+
# forwarder method name
|
185
|
+
# @param _args [Array<Object>]
|
186
|
+
# additional arguments
|
187
|
+
# @return [void]
|
188
|
+
def forward_proc_receiver(proc, method, name, *_args)
|
189
|
+
define_method(name) do |*args, &b|
|
190
|
+
instance_eval(&proc).__send__(method, *_args, *args, &b)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
# Forward the name to the identity function.
|
195
|
+
#
|
196
|
+
# @param name [Symbol]
|
197
|
+
# forwarder method name
|
198
|
+
# @return [void]
|
199
|
+
def forward_identity_receiver(name)
|
200
|
+
define_method(name) do |*args, &b|
|
201
|
+
self
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
# Forward the name to the object.
|
206
|
+
#
|
207
|
+
# @param receiver [Object]
|
208
|
+
# receiver object
|
209
|
+
# @param method [Symbol]
|
210
|
+
# method name of the object
|
211
|
+
# @param name [Symbol]
|
212
|
+
# forwarder method name
|
213
|
+
# @param _args [Array<Object>]
|
214
|
+
# additional arguments
|
215
|
+
# @return [void]
|
216
|
+
def forward_object_receiver(receiver, method, name, *_args)
|
217
|
+
define_method(name) do |*args, &b|
|
218
|
+
receiver.__send__(method, *args, &b)
|
219
|
+
end
|
220
|
+
end
|
154
221
|
end
|
155
222
|
|
156
223
|
# @api private
|
data/lib/forwardablex/version.rb
CHANGED
data/test/spec_forwardablex.rb
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'coveralls'
|
3
|
+
Coveralls.wear!
|
4
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
5
|
+
SimpleCov::Formatter::HTMLFormatter,
|
6
|
+
Coveralls::SimpleCov::Formatter
|
7
|
+
]
|
8
|
+
SimpleCov.start {add_filter 'test'}
|
9
|
+
|
1
10
|
require 'forwardable'
|
2
11
|
require 'forwardablex'
|
3
12
|
|
@@ -109,12 +118,30 @@ class KeyForwarder
|
|
109
118
|
forward_as_key :@table, :a
|
110
119
|
forward_as_key :@table, :a, :b
|
111
120
|
forward_as_key! :@table, :c, :d, :e
|
121
|
+
forward_as_key :class, :f
|
122
|
+
forward_as_key Proc.new{@table}, :g
|
123
|
+
forward_as_key Receiver.new, :h
|
124
|
+
|
125
|
+
class << self
|
126
|
+
def [](key)
|
127
|
+
{f: 1}[key]
|
128
|
+
end
|
129
|
+
end
|
112
130
|
|
113
131
|
def initialize(table={})
|
114
132
|
@table = table
|
115
133
|
end
|
116
134
|
end
|
117
135
|
|
136
|
+
class IdentityForwarder
|
137
|
+
forward :identity, :a
|
138
|
+
forward :identity, :a, :b
|
139
|
+
forward! :identity, :c, :d, :e
|
140
|
+
forward_as_key :identity, :f
|
141
|
+
forward_as_key :identity, :f, :g
|
142
|
+
forward_as_key! :identity, :h, :i, :j
|
143
|
+
end
|
144
|
+
|
118
145
|
describe 'ForwardableX' do
|
119
146
|
[ XForwarder.new,
|
120
147
|
DefDelegatorForwarder.new,
|
@@ -150,12 +177,29 @@ describe 'ForwardableX' do
|
|
150
177
|
end
|
151
178
|
|
152
179
|
it 'should forward to the table as key' do
|
153
|
-
KeyForwarder.new(a: 1, b: 2, c: 3, d: 4, e: 5).tap do |obj|
|
180
|
+
KeyForwarder.new(a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8).tap do |obj|
|
154
181
|
obj.a.should == 1
|
155
182
|
obj.b.should == 1
|
156
183
|
obj.c.should == 3
|
157
184
|
obj.d.should == 4
|
158
185
|
obj.e.should == 5
|
186
|
+
obj.f.should == 1
|
187
|
+
obj.g.should == 7
|
188
|
+
obj.h.should == :[]
|
159
189
|
end
|
160
190
|
end
|
191
|
+
|
192
|
+
it 'should forward to identity' do
|
193
|
+
forwarder = IdentityForwarder.new
|
194
|
+
forwarder.a.should == forwarder
|
195
|
+
forwarder.b.should == forwarder
|
196
|
+
forwarder.c.should == forwarder
|
197
|
+
forwarder.d.should == forwarder
|
198
|
+
forwarder.e.should == forwarder
|
199
|
+
forwarder.f.should == forwarder
|
200
|
+
forwarder.g.should == forwarder
|
201
|
+
forwarder.h.should == forwarder
|
202
|
+
forwarder.i.should == forwarder
|
203
|
+
forwarder.j.should == forwarder
|
204
|
+
end
|
161
205
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: forwardablex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: bacon
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -32,17 +48,17 @@ dependencies:
|
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
33
49
|
none: false
|
34
50
|
requirements:
|
35
|
-
- -
|
51
|
+
- - ! '>='
|
36
52
|
- !ruby/object:Gem::Version
|
37
|
-
version: 0
|
53
|
+
version: '0'
|
38
54
|
type: :development
|
39
55
|
prerelease: false
|
40
56
|
version_requirements: !ruby/object:Gem::Requirement
|
41
57
|
none: false
|
42
58
|
requirements:
|
43
|
-
- -
|
59
|
+
- - ! '>='
|
44
60
|
- !ruby/object:Gem::Version
|
45
|
-
version: 0
|
61
|
+
version: '0'
|
46
62
|
- !ruby/object:Gem::Dependency
|
47
63
|
name: redcarpet
|
48
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,6 +83,7 @@ extensions: []
|
|
67
83
|
extra_rdoc_files: []
|
68
84
|
files:
|
69
85
|
- .gitignore
|
86
|
+
- .travis.yml
|
70
87
|
- Gemfile
|
71
88
|
- HISTORY.md
|
72
89
|
- LICENSE.txt
|
@@ -96,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
113
|
version: '0'
|
97
114
|
requirements: []
|
98
115
|
rubyforge_project:
|
99
|
-
rubygems_version: 1.8.
|
116
|
+
rubygems_version: 1.8.25
|
100
117
|
signing_key:
|
101
118
|
specification_version: 3
|
102
119
|
summary: Forwardable extension
|