rubysl-forwardable 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +10 -3
- data/lib/rubysl/forwardable/forwardable.rb +118 -99
- data/lib/rubysl/forwardable/version.rb +1 -1
- data/rubysl-forwardable.gemspec +4 -1
- metadata +28 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1716ce3c82dd24107c6676b126a1cf852e630791
|
4
|
+
data.tar.gz: 7044d2cc7f42591cb6ce97f3b9b4b135de895ea5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e68085a41e419c75c5feb9e6c7bf36007f647782fa26eaa1b5cc65ec88e6db765ae717c1074dbd3b4effad4df6810e87bbd10239dad3eb2c5144c21f7ee12835
|
7
|
+
data.tar.gz: 2276f86a3bc797e239b5bfdbf84c4a8a5fbe6865f8d39ae3f73d375dc134b416c1b9d4cf1cff5e0b5f60eb8438994cf0d81f77300cee3bdfdd14a1a530ce47e6
|
data/.travis.yml
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
language: ruby
|
2
2
|
env:
|
3
3
|
- RUBYLIB=lib
|
4
|
-
|
4
|
+
- RUBYLIB=
|
5
|
+
script: mspec spec
|
5
6
|
rvm:
|
6
|
-
-
|
7
|
-
- rbx-
|
7
|
+
- 2.0.0
|
8
|
+
- rbx-2.1.1
|
9
|
+
matrix:
|
10
|
+
exclude:
|
11
|
+
- rvm: 2.0.0
|
12
|
+
env: RUBYLIB=lib
|
13
|
+
- rvm: rbx-2.1.1
|
14
|
+
env: RUBYLIB=
|
@@ -1,35 +1,65 @@
|
|
1
1
|
#
|
2
2
|
# forwardable.rb -
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
3
|
+
# $Release Version: 1.1$
|
4
|
+
# $Revision: 40906 $
|
5
|
+
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
6
|
+
# original definition by delegator.rb
|
7
7
|
# Revised by Daniel J. Berger with suggestions from Florian Gross.
|
8
8
|
#
|
9
9
|
# Documentation by James Edward Gray II and Gavin Sinclair
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
# The Forwardable module provides delegation of specified
|
14
|
+
# methods to a designated object, using the methods #def_delegator
|
15
|
+
# and #def_delegators.
|
10
16
|
#
|
11
|
-
#
|
17
|
+
# For example, say you have a class RecordCollection which
|
18
|
+
# contains an array <tt>@records</tt>. You could provide the lookup method
|
19
|
+
# #record_number(), which simply calls #[] on the <tt>@records</tt>
|
20
|
+
# array, like this:
|
12
21
|
#
|
13
|
-
#
|
14
|
-
# method basis.
|
22
|
+
# require 'forwardable'
|
15
23
|
#
|
16
|
-
#
|
24
|
+
# class RecordCollection
|
25
|
+
# attr_accessor :records
|
26
|
+
# extend Forwardable
|
27
|
+
# def_delegator :@records, :[], :record_number
|
28
|
+
# end
|
17
29
|
#
|
18
|
-
#
|
30
|
+
# We can use the lookup method like so:
|
31
|
+
#
|
32
|
+
# r = RecordCollection.new
|
33
|
+
# r.records = [4,5,6]
|
34
|
+
# r.record_number(0) # => 4
|
35
|
+
#
|
36
|
+
# Further, if you wish to provide the methods #size, #<<, and #map,
|
37
|
+
# all of which delegate to @records, this is how you can do it:
|
38
|
+
#
|
39
|
+
# class RecordCollection # re-open RecordCollection class
|
40
|
+
# def_delegators :@records, :size, :<<, :map
|
41
|
+
# end
|
19
42
|
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
43
|
+
# r = RecordCollection.new
|
44
|
+
# r.records = [1,2,3]
|
45
|
+
# r.record_number(0) # => 1
|
46
|
+
# r.size # => 3
|
47
|
+
# r << 4 # => [1, 2, 3, 4]
|
48
|
+
# r.map { |x| x * 2 } # => [2, 4, 6, 8]
|
23
49
|
#
|
24
|
-
#
|
50
|
+
# You can even extend regular objects with Forwardable.
|
25
51
|
#
|
26
|
-
#
|
52
|
+
# my_hash = Hash.new
|
53
|
+
# my_hash.extend Forwardable # prepare object for delegation
|
54
|
+
# my_hash.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
55
|
+
# my_hash.puts "Howdy!"
|
27
56
|
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
57
|
+
# == Another example
|
58
|
+
#
|
59
|
+
# We want to rely on what has come before obviously, but with delegation we can
|
60
|
+
# take just the methods we need and even rename them as appropriate. In many
|
61
|
+
# cases this is preferable to inheritance, which gives us the entire old
|
62
|
+
# interface, even if much of it isn't needed.
|
33
63
|
#
|
34
64
|
# class Queue
|
35
65
|
# extend Forwardable
|
@@ -60,7 +90,7 @@
|
|
60
90
|
# q.clear
|
61
91
|
# puts q.first
|
62
92
|
#
|
63
|
-
#
|
93
|
+
# This should output:
|
64
94
|
#
|
65
95
|
# 2
|
66
96
|
# 3
|
@@ -70,72 +100,24 @@
|
|
70
100
|
# Ruby
|
71
101
|
# nil
|
72
102
|
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
# printer = String.new
|
76
|
-
# printer.extend SingleForwardable # prepare object for delegation
|
77
|
-
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
78
|
-
# printer.puts "Howdy!"
|
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
|
-
#
|
101
|
-
# <i>Prints:</i>
|
102
|
-
#
|
103
|
-
# Howdy!
|
104
|
-
|
105
|
-
#
|
106
|
-
# The Forwardable module provides delegation of specified
|
107
|
-
# methods to a designated object, using the methods #def_delegator
|
108
|
-
# and #def_delegators.
|
109
|
-
#
|
110
|
-
# For example, say you have a class RecordCollection which
|
111
|
-
# contains an array <tt>@records</tt>. You could provide the lookup method
|
112
|
-
# #record_number(), which simply calls #[] on the <tt>@records</tt>
|
113
|
-
# array, like this:
|
114
|
-
#
|
115
|
-
# class RecordCollection
|
116
|
-
# extend Forwardable
|
117
|
-
# def_delegator :@records, :[], :record_number
|
118
|
-
# end
|
103
|
+
# == Notes
|
119
104
|
#
|
120
|
-
#
|
121
|
-
# all of which delegate to @records, this is how you can do it:
|
105
|
+
# Be advised, RDoc will not detect delegated methods.
|
122
106
|
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
# end
|
127
|
-
# f = Foo.new
|
128
|
-
# f.printf ...
|
129
|
-
# f.gets
|
130
|
-
# f.content_at(1)
|
107
|
+
# +forwardable.rb+ provides single-method delegation via the def_delegator and
|
108
|
+
# def_delegators methods. For full-class delegation via DelegateClass, see
|
109
|
+
# +delegate.rb+.
|
131
110
|
#
|
132
|
-
# Also see the example at forwardable.rb.
|
133
|
-
|
134
111
|
module Forwardable
|
112
|
+
# Version of +forwardable.rb+
|
135
113
|
FORWARDABLE_VERSION = "1.1.0"
|
136
114
|
|
115
|
+
FILE_REGEXP = %r"#{Regexp.quote(__FILE__)}"
|
116
|
+
|
137
117
|
@debug = nil
|
138
118
|
class << self
|
119
|
+
# If true, <tt>__FILE__</tt> will remain in the backtrace in the event an
|
120
|
+
# Exception is raised.
|
139
121
|
attr_accessor :debug
|
140
122
|
end
|
141
123
|
|
@@ -175,15 +157,34 @@ module Forwardable
|
|
175
157
|
end
|
176
158
|
end
|
177
159
|
|
160
|
+
# Define +method+ as delegator instance method with an optional
|
161
|
+
# alias name +ali+. Method calls to +ali+ will be delegated to
|
162
|
+
# +accessor.method+.
|
163
|
+
#
|
164
|
+
# class MyQueue
|
165
|
+
# extend Forwardable
|
166
|
+
# attr_reader :queue
|
167
|
+
# def initialize
|
168
|
+
# @queue = []
|
169
|
+
# end
|
170
|
+
#
|
171
|
+
# def_delegator :@queue, :push, :mypush
|
172
|
+
# end
|
173
|
+
#
|
174
|
+
# q = MyQueue.new
|
175
|
+
# q.mypush 42
|
176
|
+
# q.queue #=> [42]
|
177
|
+
# q.push 23 #=> NoMethodError
|
178
|
+
#
|
178
179
|
def def_instance_delegator(accessor, method, ali = method)
|
179
180
|
line_no = __LINE__; str = %{
|
180
181
|
def #{ali}(*args, &block)
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
182
|
+
begin
|
183
|
+
#{accessor}.__send__(:#{method}, *args, &block)
|
184
|
+
rescue Exception
|
185
|
+
$@.delete_if{|s| Forwardable::FILE_REGEXP =~ s} unless Forwardable::debug
|
186
|
+
::Kernel::raise
|
187
|
+
end
|
187
188
|
end
|
188
189
|
}
|
189
190
|
# If it's not a class or module, it's an instance
|
@@ -200,9 +201,30 @@ module Forwardable
|
|
200
201
|
alias def_delegator def_instance_delegator
|
201
202
|
end
|
202
203
|
|
204
|
+
# SingleForwardable can be used to setup delegation at the object level as well.
|
205
|
+
#
|
206
|
+
# printer = String.new
|
207
|
+
# printer.extend SingleForwardable # prepare object for delegation
|
208
|
+
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
209
|
+
# printer.puts "Howdy!"
|
210
|
+
#
|
211
|
+
# Also, SingleForwardable can be used to set up delegation for a Class or Module.
|
212
|
+
#
|
213
|
+
# class Implementation
|
214
|
+
# def self.service
|
215
|
+
# puts "serviced!"
|
216
|
+
# end
|
217
|
+
# end
|
203
218
|
#
|
204
|
-
#
|
219
|
+
# module Facade
|
220
|
+
# extend SingleForwardable
|
221
|
+
# def_delegator :Implementation, :service
|
222
|
+
# end
|
223
|
+
#
|
224
|
+
# Facade.service #=> serviced!
|
205
225
|
#
|
226
|
+
# If you want to use both Forwardable and SingleForwardable, you can
|
227
|
+
# use methods def_instance_delegator and def_single_delegator, etc.
|
206
228
|
module SingleForwardable
|
207
229
|
# Takes a hash as its argument. The key is a symbol or an array of
|
208
230
|
# symbols. These symbols correspond to method names. The value is
|
@@ -240,20 +262,21 @@ module SingleForwardable
|
|
240
262
|
end
|
241
263
|
end
|
242
264
|
|
265
|
+
# :call-seq:
|
266
|
+
# def_single_delegator(accessor, method, new_name=method)
|
243
267
|
#
|
244
|
-
# Defines a method _method_ which delegates to
|
245
|
-
# the method of the same name in
|
268
|
+
# Defines a method _method_ which delegates to _accessor_ (i.e. it calls
|
269
|
+
# the method of the same name in _accessor_). If _new_name_ is
|
246
270
|
# provided, it is used as the name for the delegate method.
|
247
|
-
#
|
248
271
|
def def_single_delegator(accessor, method, ali = method)
|
249
|
-
|
272
|
+
str = %{
|
250
273
|
def #{ali}(*args, &block)
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
274
|
+
begin
|
275
|
+
#{accessor}.__send__(:#{method}, *args, &block)
|
276
|
+
rescue Exception
|
277
|
+
$@.delete_if{|s| Forwardable::FILE_REGEXP =~ s} unless Forwardable::debug
|
278
|
+
::Kernel::raise
|
279
|
+
end
|
257
280
|
end
|
258
281
|
}
|
259
282
|
|
@@ -264,7 +287,3 @@ module SingleForwardable
|
|
264
287
|
alias def_delegators def_single_delegators
|
265
288
|
alias def_delegator def_single_delegator
|
266
289
|
end
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
data/rubysl-forwardable.gemspec
CHANGED
@@ -16,7 +16,10 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
17
|
spec.require_paths = ["lib"]
|
18
18
|
|
19
|
+
spec.required_ruby_version = "~> 2.0"
|
20
|
+
|
19
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
20
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
21
23
|
spec.add_development_dependency "mspec", "~> 1.5"
|
22
|
-
|
24
|
+
spec.add_development_dependency "rubysl-prettyprint", "~> 2.0"
|
25
|
+
end
|
metadata
CHANGED
@@ -1,57 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubysl-forwardable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
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-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: mspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '1.5'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
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: '2.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
55
69
|
description: Ruby standard library forwardable.
|
56
70
|
email:
|
57
71
|
- brixen@gmail.com
|
@@ -59,8 +73,8 @@ executables: []
|
|
59
73
|
extensions: []
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
62
|
-
- .gitignore
|
63
|
-
- .travis.yml
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
64
78
|
- Gemfile
|
65
79
|
- LICENSE
|
66
80
|
- README.md
|
@@ -80,12 +94,12 @@ require_paths:
|
|
80
94
|
- lib
|
81
95
|
required_ruby_version: !ruby/object:Gem::Requirement
|
82
96
|
requirements:
|
83
|
-
- -
|
97
|
+
- - "~>"
|
84
98
|
- !ruby/object:Gem::Version
|
85
|
-
version: '0'
|
99
|
+
version: '2.0'
|
86
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
101
|
requirements:
|
88
|
-
- -
|
102
|
+
- - ">="
|
89
103
|
- !ruby/object:Gem::Version
|
90
104
|
version: '0'
|
91
105
|
requirements: []
|
@@ -95,3 +109,4 @@ signing_key:
|
|
95
109
|
specification_version: 4
|
96
110
|
summary: Ruby standard library forwardable.
|
97
111
|
test_files: []
|
112
|
+
has_rdoc:
|