forwardablex 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +4 -0
- data/README.md +13 -0
- data/lib/forwardablex/version.rb +1 -1
- data/lib/forwardablex.rb +45 -0
- data/test/spec_forwardablex.rb +20 -0
- metadata +3 -3
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -55,6 +55,19 @@ end
|
|
55
55
|
Forwarder.new.m #=> "forwarded"
|
56
56
|
```
|
57
57
|
|
58
|
+
### Table Accessor
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class Forwarder
|
62
|
+
forward_as_key :@table, :key
|
63
|
+
|
64
|
+
def initialize
|
65
|
+
@table = {:key => "forwarded"}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
Forwarder.new.key # => "forwarded"
|
69
|
+
```
|
70
|
+
|
58
71
|
## Licence
|
59
72
|
|
60
73
|
ForwardableX is free software distributed under MIT licence.
|
data/lib/forwardablex/version.rb
CHANGED
data/lib/forwardablex.rb
CHANGED
@@ -106,6 +106,51 @@ module ForwardableX
|
|
106
106
|
forward!(receiver, *methods)
|
107
107
|
end
|
108
108
|
end
|
109
|
+
|
110
|
+
# Define a method that forwards the key to the receiver. You can specify
|
111
|
+
# receiver as instance variable name, Proc object, and plain object.
|
112
|
+
#
|
113
|
+
# @param receiver [Symbol, String, Proc, or Object]
|
114
|
+
# message receiver that have method #[]
|
115
|
+
# @param key [Symbol]
|
116
|
+
# key that we forward to the receiver
|
117
|
+
# @param name [Symbol]
|
118
|
+
# method name that we forward from
|
119
|
+
# @return [void]
|
120
|
+
def forward_as_key(receiver, key, name=key)
|
121
|
+
context = self.kind_of?(Module) ? self : self.singleton_class
|
122
|
+
context.instance_eval do
|
123
|
+
case receiver
|
124
|
+
when :class
|
125
|
+
define_method(name) do |*args, &b|
|
126
|
+
self.class.__send__(:[], key, *args, &b)
|
127
|
+
end
|
128
|
+
when Symbol, String
|
129
|
+
define_method(name) do |*args, &b|
|
130
|
+
instance_variable_get(receiver).__send__(:[], key, *args, &b)
|
131
|
+
end
|
132
|
+
when Proc
|
133
|
+
define_method(name) do |*args, &b|
|
134
|
+
instance_eval(&receiver).__send__(:[], key, *args, &b)
|
135
|
+
end
|
136
|
+
else
|
137
|
+
define_method(name) do |*args, &b|
|
138
|
+
receiver.__send__(:[], key, *args, &b)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# Define each method that forwards to the receiver as key.
|
145
|
+
#
|
146
|
+
# @param receiver [Symbol, String, Proc, or Object]
|
147
|
+
# message receiver that have method #[]
|
148
|
+
# @param key [Array<Symbol>]
|
149
|
+
# key that we forward to the receiver
|
150
|
+
# @return [void]
|
151
|
+
def forward_as_key!(receiver, *keys)
|
152
|
+
keys.each {|key| forward_as_key(receiver, key)}
|
153
|
+
end
|
109
154
|
end
|
110
155
|
|
111
156
|
# @api private
|
data/test/spec_forwardablex.rb
CHANGED
@@ -105,6 +105,16 @@ class ClassMethodForwarderA < ClassMethodForwarder
|
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
+
class KeyForwarder
|
109
|
+
forward_as_key :@table, :a
|
110
|
+
forward_as_key :@table, :a, :b
|
111
|
+
forward_as_key! :@table, :c, :d, :e
|
112
|
+
|
113
|
+
def initialize(table={})
|
114
|
+
@table = table
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
108
118
|
describe 'ForwardableX' do
|
109
119
|
[ XForwarder.new,
|
110
120
|
DefDelegatorForwarder.new,
|
@@ -138,4 +148,14 @@ describe 'ForwardableX' do
|
|
138
148
|
ClassMethodForwarder.new.m.should == "forwarded"
|
139
149
|
ClassMethodForwarderA.new.m.should == "forwarded to A"
|
140
150
|
end
|
151
|
+
|
152
|
+
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|
|
154
|
+
obj.a.should == 1
|
155
|
+
obj.b.should == 1
|
156
|
+
obj.c.should == 3
|
157
|
+
obj.d.should == 4
|
158
|
+
obj.e.should == 5
|
159
|
+
end
|
160
|
+
end
|
141
161
|
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.3
|
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: 2013-04-
|
12
|
+
date: 2013-04-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bacon
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
98
|
rubyforge_project:
|
99
|
-
rubygems_version: 1.8.
|
99
|
+
rubygems_version: 1.8.24
|
100
100
|
signing_key:
|
101
101
|
specification_version: 3
|
102
102
|
summary: Forwardable extension
|