rubysl-forwardable 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +5 -6
- data/lib/rubysl/forwardable/forwardable.rb +121 -69
- data/lib/rubysl/forwardable/version.rb +1 -1
- data/rubysl-forwardable.gemspec +1 -2
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62ca4e7f89156c2691cd34cefde13fa0c6ad7d84
|
4
|
+
data.tar.gz: df4d7a3df9c54e3855c610f4691535d83eb6b7f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d9d5fdc5ff98d24a346a1be08fe93dcb9f81479512037a1fda56392586a0886d09bacc5feb2dd5092cf89aae2396daf6dab0247e33155b0839bae92481a42a5
|
7
|
+
data.tar.gz: 1dfb2806a207e15079b467cd8d4a822a29a2e6e39487bf74e632cd15e4b73f875d3153ae7428f46973bd9095cbe7cb5dea032838c9bd49d9b54572c2425e480f
|
data/.travis.yml
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
# = forwardable - Support for the Delegation Pattern
|
2
1
|
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
2
|
+
# forwardable.rb -
|
3
|
+
# $Release Version: 1.1$
|
4
|
+
# $Revision: 31833 $
|
5
|
+
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
6
|
+
# original definition by delegator.rb
|
7
|
+
# Revised by Daniel J. Berger with suggestions from Florian Gross.
|
7
8
|
#
|
8
|
-
#
|
9
|
+
# Documentation by James Edward Gray II and Gavin Sinclair
|
9
10
|
#
|
10
11
|
# == Introduction
|
11
12
|
#
|
12
13
|
# This library allows you delegate method calls to an object, on a method by
|
13
|
-
# method basis.
|
14
|
-
# level, or SingleForwardable to handle it at the object level.
|
14
|
+
# method basis.
|
15
15
|
#
|
16
16
|
# == Notes
|
17
17
|
#
|
@@ -33,28 +33,28 @@
|
|
33
33
|
#
|
34
34
|
# class Queue
|
35
35
|
# extend Forwardable
|
36
|
-
#
|
36
|
+
#
|
37
37
|
# def initialize
|
38
38
|
# @q = [ ] # prepare delegate object
|
39
39
|
# end
|
40
|
-
#
|
41
|
-
# # setup
|
40
|
+
#
|
41
|
+
# # setup preferred interface, enq() and deq()...
|
42
42
|
# def_delegator :@q, :push, :enq
|
43
43
|
# def_delegator :@q, :shift, :deq
|
44
|
-
#
|
44
|
+
#
|
45
45
|
# # support some general Array methods that fit Queues well
|
46
46
|
# def_delegators :@q, :clear, :first, :push, :shift, :size
|
47
47
|
# end
|
48
|
-
#
|
48
|
+
#
|
49
49
|
# q = Queue.new
|
50
50
|
# q.enq 1, 2, 3, 4, 5
|
51
51
|
# q.push 6
|
52
|
-
#
|
52
|
+
#
|
53
53
|
# q.shift # => 1
|
54
54
|
# while q.size > 0
|
55
55
|
# puts q.deq
|
56
56
|
# end
|
57
|
-
#
|
57
|
+
#
|
58
58
|
# q.enq "Ruby", "Perl", "Python"
|
59
59
|
# puts q.first
|
60
60
|
# q.clear
|
@@ -70,13 +70,34 @@
|
|
70
70
|
# Ruby
|
71
71
|
# nil
|
72
72
|
#
|
73
|
-
#
|
73
|
+
# SingleForwardable can be used to setup delegation at the object level as well.
|
74
74
|
#
|
75
75
|
# printer = String.new
|
76
76
|
# printer.extend SingleForwardable # prepare object for delegation
|
77
77
|
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
78
78
|
# printer.puts "Howdy!"
|
79
79
|
#
|
80
|
+
# Also, SingleForwardable can be use to Class or Module.
|
81
|
+
#
|
82
|
+
# module Facade
|
83
|
+
# extend SingleForwardable
|
84
|
+
# def_delegator :Implementation, :service
|
85
|
+
#
|
86
|
+
# class Implementation
|
87
|
+
# def service...
|
88
|
+
# end
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# If you want to use both Forwardable and SingleForwardable, you can
|
92
|
+
# use methods def_instance_delegator and def_single_delegator, etc.
|
93
|
+
#
|
94
|
+
# If the object isn't a Module and Class, You can too extend
|
95
|
+
# Forwardable module.
|
96
|
+
# printer = String.new
|
97
|
+
# printer.extend Forwardable # prepare object for delegation
|
98
|
+
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
99
|
+
# printer.puts "Howdy!"
|
100
|
+
#
|
80
101
|
# <i>Prints:</i>
|
81
102
|
#
|
82
103
|
# Howdy!
|
@@ -103,17 +124,38 @@
|
|
103
124
|
# # extend Forwardable, but we did that above
|
104
125
|
# def_delegators :@records, :size, :<<, :map
|
105
126
|
# end
|
127
|
+
# f = Foo.new
|
128
|
+
# f.printf ...
|
129
|
+
# f.gets
|
130
|
+
# f.content_at(1)
|
106
131
|
#
|
107
132
|
# Also see the example at forwardable.rb.
|
108
|
-
|
133
|
+
|
109
134
|
module Forwardable
|
135
|
+
FORWARDABLE_VERSION = "1.1.0"
|
110
136
|
|
111
137
|
@debug = nil
|
112
|
-
class<<self
|
113
|
-
# force Forwardable to show up in stack backtraces of delegated methods
|
138
|
+
class << self
|
114
139
|
attr_accessor :debug
|
115
140
|
end
|
116
141
|
|
142
|
+
# Takes a hash as its argument. The key is a symbol or an array of
|
143
|
+
# symbols. These symbols correspond to method names. The value is
|
144
|
+
# the accessor to which the methods will be delegated.
|
145
|
+
#
|
146
|
+
# :call-seq:
|
147
|
+
# delegate method => accessor
|
148
|
+
# delegate [method, method, ...] => accessor
|
149
|
+
#
|
150
|
+
def instance_delegate(hash)
|
151
|
+
hash.each{ |methods, accessor|
|
152
|
+
methods = [methods] unless methods.respond_to?(:each)
|
153
|
+
methods.each{ |method|
|
154
|
+
def_instance_delegator(accessor, method)
|
155
|
+
}
|
156
|
+
}
|
157
|
+
end
|
158
|
+
|
117
159
|
#
|
118
160
|
# Shortcut for defining multiple delegator methods, but with no
|
119
161
|
# provision for using a different name. The following two code
|
@@ -125,67 +167,76 @@ module Forwardable
|
|
125
167
|
# def_delegator :@records, :<<
|
126
168
|
# def_delegator :@records, :map
|
127
169
|
#
|
128
|
-
# See the examples at Forwardable and forwardable.rb.
|
129
|
-
#
|
130
170
|
def def_instance_delegators(accessor, *methods)
|
171
|
+
methods.delete("__send__")
|
172
|
+
methods.delete("__id__")
|
131
173
|
for method in methods
|
132
174
|
def_instance_delegator(accessor, method)
|
133
175
|
end
|
134
176
|
end
|
135
177
|
|
136
|
-
#
|
137
|
-
# Defines a method _method_ which delegates to _obj_ (i.e. it calls
|
138
|
-
# the method of the same name in _obj_). If _new_name_ is
|
139
|
-
# provided, it is used as the name for the delegate method.
|
140
|
-
#
|
141
|
-
# See the examples at Forwardable and forwardable.rb.
|
142
|
-
#
|
143
178
|
def def_instance_delegator(accessor, method, ali = method)
|
144
|
-
|
145
|
-
method = method.id2name if method.kind_of?(Integer)
|
146
|
-
ali = ali.id2name if ali.kind_of?(Integer)
|
147
|
-
|
148
|
-
module_eval(<<-EOS, "(__FORWARDABLE__)", 1)
|
179
|
+
line_no = __LINE__; str = %{
|
149
180
|
def #{ali}(*args, &block)
|
150
181
|
begin
|
151
182
|
#{accessor}.__send__(:#{method}, *args, &block)
|
152
183
|
rescue Exception
|
153
|
-
$@.delete_if{|s|
|
154
|
-
Kernel::raise
|
184
|
+
$@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
|
185
|
+
::Kernel::raise
|
155
186
|
end
|
156
187
|
end
|
157
|
-
|
188
|
+
}
|
189
|
+
# If it's not a class or module, it's an instance
|
190
|
+
begin
|
191
|
+
module_eval(str, __FILE__, line_no)
|
192
|
+
rescue
|
193
|
+
instance_eval(str, __FILE__, line_no)
|
194
|
+
end
|
195
|
+
|
158
196
|
end
|
159
197
|
|
198
|
+
alias delegate instance_delegate
|
160
199
|
alias def_delegators def_instance_delegators
|
161
200
|
alias def_delegator def_instance_delegator
|
162
201
|
end
|
163
202
|
|
164
203
|
#
|
165
|
-
# The SingleForwardable
|
166
|
-
# methods to a designated object, using the methods #def_delegator
|
167
|
-
# and #def_delegators. This module is similar to Forwardable, but it works on
|
168
|
-
# objects themselves, instead of their defining classes.
|
169
|
-
#
|
170
|
-
# Also see the example at forwardable.rb.
|
204
|
+
# Usage of The SingleForwardable is like Fowadable module.
|
171
205
|
#
|
172
206
|
module SingleForwardable
|
207
|
+
# Takes a hash as its argument. The key is a symbol or an array of
|
208
|
+
# symbols. These symbols correspond to method names. The value is
|
209
|
+
# the accessor to which the methods will be delegated.
|
210
|
+
#
|
211
|
+
# :call-seq:
|
212
|
+
# delegate method => accessor
|
213
|
+
# delegate [method, method, ...] => accessor
|
214
|
+
#
|
215
|
+
def single_delegate(hash)
|
216
|
+
hash.each{ |methods, accessor|
|
217
|
+
methods = [methods] unless methods.respond_to?(:each)
|
218
|
+
methods.each{ |method|
|
219
|
+
def_single_delegator(accessor, method)
|
220
|
+
}
|
221
|
+
}
|
222
|
+
end
|
223
|
+
|
173
224
|
#
|
174
225
|
# Shortcut for defining multiple delegator methods, but with no
|
175
226
|
# provision for using a different name. The following two code
|
176
227
|
# samples have the same effect:
|
177
228
|
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
# single_forwardable.def_delegator :@records, :size
|
181
|
-
# single_forwardable.def_delegator :@records, :<<
|
182
|
-
# single_forwardable.def_delegator :@records, :map
|
229
|
+
# def_delegators :@records, :size, :<<, :map
|
183
230
|
#
|
184
|
-
#
|
231
|
+
# def_delegator :@records, :size
|
232
|
+
# def_delegator :@records, :<<
|
233
|
+
# def_delegator :@records, :map
|
185
234
|
#
|
186
|
-
def
|
235
|
+
def def_single_delegators(accessor, *methods)
|
236
|
+
methods.delete("__send__")
|
237
|
+
methods.delete("__id__")
|
187
238
|
for method in methods
|
188
|
-
|
239
|
+
def_single_delegator(accessor, method)
|
189
240
|
end
|
190
241
|
end
|
191
242
|
|
@@ -194,25 +245,26 @@ module SingleForwardable
|
|
194
245
|
# the method of the same name in _obj_). If _new_name_ is
|
195
246
|
# provided, it is used as the name for the delegate method.
|
196
247
|
#
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
Kernel::raise
|
211
|
-
end
|
212
|
-
end
|
213
|
-
EOS
|
248
|
+
def def_single_delegator(accessor, method, ali = method)
|
249
|
+
line_no = __LINE__; str = %{
|
250
|
+
def #{ali}(*args, &block)
|
251
|
+
begin
|
252
|
+
#{accessor}.__send__(:#{method}, *args, &block)
|
253
|
+
rescue Exception
|
254
|
+
$@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
|
255
|
+
::Kernel::raise
|
256
|
+
end
|
257
|
+
end
|
258
|
+
}
|
259
|
+
|
260
|
+
instance_eval(str, __FILE__, __LINE__)
|
214
261
|
end
|
215
262
|
|
216
|
-
alias
|
217
|
-
alias
|
263
|
+
alias delegate single_delegate
|
264
|
+
alias def_delegators def_single_delegators
|
265
|
+
alias def_delegator def_single_delegator
|
218
266
|
end
|
267
|
+
|
268
|
+
|
269
|
+
|
270
|
+
|
data/rubysl-forwardable.gemspec
CHANGED
@@ -19,5 +19,4 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.add_development_dependency "bundler", "~> 1.3"
|
20
20
|
spec.add_development_dependency "rake", "~> 10.0"
|
21
21
|
spec.add_development_dependency "mspec", "~> 1.5"
|
22
|
-
|
23
|
-
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubysl-forwardable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Shirai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.5'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rubysl-prettyprint
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '1.0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ~>
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '1.0'
|
69
55
|
description: Ruby standard library forwardable.
|
70
56
|
email:
|
71
57
|
- brixen@gmail.com
|