forwardable 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/forwardable.gemspec +3 -6
- data/lib/forwardable.rb +21 -18
- data/lib/forwardable/impl.rb +0 -8
- data/lib/forwardable/version.rb +5 -0
- metadata +5 -41
- data/.gitignore +0 -8
- data/.travis.yml +0 -6
- data/Gemfile +0 -5
- data/LICENSE.txt +0 -22
- data/README.md +0 -79
- data/Rakefile +0 -10
- data/bin/console +0 -7
- data/bin/setup +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b280e81b85cc1b7991cae97589999483f7e7e8fcf0296f4c40328504a9da9a3
|
4
|
+
data.tar.gz: b96bcbf1ee2f712336e260ce5ad71f6d54c65e6133278feb10c93e8cb4b0a373
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d6487cca72bb5f315d638733a34f4ff553c53cb1caa84938ca639d746b9defe76bdc37381afce3dacb43331a6cd69f7cb6fa489170167c1b4777f8c9d90443a
|
7
|
+
data.tar.gz: f65f9418fd7c1c9c3b27adb398768f41bd5c04479b8306f2ec454a4c9c98e10eaf933d92db64b279e4c433e19fea4d5b7ae4a5da6f2ac9a342e50c175bebdb9f
|
data/forwardable.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
begin
|
2
|
-
require_relative "lib/forwardable"
|
2
|
+
require_relative "lib/forwardable/version"
|
3
3
|
rescue LoadError
|
4
4
|
# for Ruby core repository
|
5
|
-
require_relative "
|
5
|
+
require_relative "version"
|
6
6
|
end
|
7
7
|
|
8
8
|
Gem::Specification.new do |spec|
|
@@ -16,11 +16,8 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.homepage = "https://github.com/ruby/forwardable"
|
17
17
|
spec.license = "BSD-2-Clause"
|
18
18
|
|
19
|
-
spec.files = [".
|
19
|
+
spec.files = ["forwardable.gemspec", "lib/forwardable.rb", "lib/forwardable/impl.rb", "lib/forwardable/version.rb"]
|
20
20
|
spec.bindir = "exe"
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ["lib"]
|
23
|
-
|
24
|
-
spec.add_development_dependency "bundler"
|
25
|
-
spec.add_development_dependency "rake"
|
26
23
|
end
|
data/lib/forwardable.rb
CHANGED
@@ -57,10 +57,9 @@
|
|
57
57
|
#
|
58
58
|
# == Another example
|
59
59
|
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
# interface, even if much of it isn't needed.
|
60
|
+
# You could use Forwardable as an alternative to inheritance, when you don't want
|
61
|
+
# to inherit all methods from the superclass. For instance, here is how you might
|
62
|
+
# add a range of +Array+ instance methods to a new class +Queue+:
|
64
63
|
#
|
65
64
|
# class Queue
|
66
65
|
# extend Forwardable
|
@@ -111,10 +110,7 @@
|
|
111
110
|
#
|
112
111
|
module Forwardable
|
113
112
|
require 'forwardable/impl'
|
114
|
-
|
115
|
-
# Version of +forwardable.rb+
|
116
|
-
VERSION = "1.2.0"
|
117
|
-
FORWARDABLE_VERSION = VERSION
|
113
|
+
require "forwardable/version"
|
118
114
|
|
119
115
|
@debug = nil
|
120
116
|
class << self
|
@@ -123,7 +119,8 @@ module Forwardable
|
|
123
119
|
end
|
124
120
|
|
125
121
|
# Takes a hash as its argument. The key is a symbol or an array of
|
126
|
-
# symbols. These symbols correspond to method names
|
122
|
+
# symbols. These symbols correspond to method names, instance variable
|
123
|
+
# names, or constant names (see def_delegator). The value is
|
127
124
|
# the accessor to which the methods will be delegated.
|
128
125
|
#
|
129
126
|
# :call-seq:
|
@@ -152,18 +149,20 @@ module Forwardable
|
|
152
149
|
# def_delegator :@records, :map
|
153
150
|
#
|
154
151
|
def def_instance_delegators(accessor, *methods)
|
155
|
-
methods.
|
156
|
-
|
157
|
-
for method in methods
|
152
|
+
methods.each do |method|
|
153
|
+
next if /\A__(?:send|id)__\z/ =~ method
|
158
154
|
def_instance_delegator(accessor, method)
|
159
155
|
end
|
160
156
|
end
|
161
157
|
|
162
158
|
# Define +method+ as delegator instance method with an optional
|
163
159
|
# alias name +ali+. Method calls to +ali+ will be delegated to
|
164
|
-
# +accessor.method+.
|
160
|
+
# +accessor.method+. +accessor+ should be a method name, instance
|
161
|
+
# variable name, or constant name. Use the full path to the
|
162
|
+
# constant if providing the constant name.
|
165
163
|
#
|
166
164
|
# class MyQueue
|
165
|
+
# CONST = 1
|
167
166
|
# extend Forwardable
|
168
167
|
# attr_reader :queue
|
169
168
|
# def initialize
|
@@ -171,18 +170,22 @@ module Forwardable
|
|
171
170
|
# end
|
172
171
|
#
|
173
172
|
# def_delegator :@queue, :push, :mypush
|
173
|
+
# def_delegator 'MyQueue::CONST', :to_i
|
174
174
|
# end
|
175
175
|
#
|
176
176
|
# q = MyQueue.new
|
177
177
|
# q.mypush 42
|
178
178
|
# q.queue #=> [42]
|
179
179
|
# q.push 23 #=> NoMethodError
|
180
|
+
# q.to_i #=> 1
|
180
181
|
#
|
181
182
|
def def_instance_delegator(accessor, method, ali = method)
|
182
183
|
gen = Forwardable._delegator_method(self, accessor, method, ali)
|
183
184
|
|
184
185
|
# If it's not a class or module, it's an instance
|
185
|
-
|
186
|
+
mod = Module === self ? self : singleton_class
|
187
|
+
mod.module_eval(&gen)
|
188
|
+
mod.send(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
|
186
189
|
end
|
187
190
|
|
188
191
|
alias delegate instance_delegate
|
@@ -222,7 +225,7 @@ module Forwardable
|
|
222
225
|
#{pre}
|
223
226
|
begin
|
224
227
|
#{accessor}
|
225
|
-
end#{method_call}
|
228
|
+
end#{method_call}
|
226
229
|
end
|
227
230
|
end
|
228
231
|
end;
|
@@ -284,9 +287,8 @@ module SingleForwardable
|
|
284
287
|
# def_delegator :@records, :map
|
285
288
|
#
|
286
289
|
def def_single_delegators(accessor, *methods)
|
287
|
-
methods.
|
288
|
-
|
289
|
-
for method in methods
|
290
|
+
methods.each do |method|
|
291
|
+
next if /\A__(?:send|id)__\z/ =~ method
|
290
292
|
def_single_delegator(accessor, method)
|
291
293
|
end
|
292
294
|
end
|
@@ -301,6 +303,7 @@ module SingleForwardable
|
|
301
303
|
gen = Forwardable._delegator_method(self, accessor, method, ali)
|
302
304
|
|
303
305
|
instance_eval(&gen)
|
306
|
+
singleton_class.send(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
|
304
307
|
end
|
305
308
|
|
306
309
|
alias delegate single_delegate
|
data/lib/forwardable/impl.rb
CHANGED
@@ -1,13 +1,5 @@
|
|
1
1
|
# :stopdoc:
|
2
2
|
module Forwardable
|
3
|
-
FILE_REGEXP = %r"#{Regexp.quote(File.dirname(__FILE__))}"
|
4
|
-
FILTER_EXCEPTION = <<-'END'
|
5
|
-
|
6
|
-
rescue ::Exception
|
7
|
-
$@.delete_if {|s| ::Forwardable::FILE_REGEXP =~ s} unless ::Forwardable::debug
|
8
|
-
::Kernel::raise
|
9
|
-
END
|
10
|
-
|
11
3
|
def self._valid_method?(method)
|
12
4
|
catch {|tag|
|
13
5
|
eval("BEGIN{throw tag}; ().#{method}", binding, __FILE__, __LINE__)
|
metadata
CHANGED
@@ -1,43 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: forwardable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Keiju ISHITSUKA
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
11
|
+
date: 2019-11-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
41
13
|
description: Provides delegation of specified methods to a designated object.
|
42
14
|
email:
|
43
15
|
- keiju@ruby-lang.org
|
@@ -45,17 +17,10 @@ executables: []
|
|
45
17
|
extensions: []
|
46
18
|
extra_rdoc_files: []
|
47
19
|
files:
|
48
|
-
- ".gitignore"
|
49
|
-
- ".travis.yml"
|
50
|
-
- Gemfile
|
51
|
-
- LICENSE.txt
|
52
|
-
- README.md
|
53
|
-
- Rakefile
|
54
|
-
- bin/console
|
55
|
-
- bin/setup
|
56
20
|
- forwardable.gemspec
|
57
21
|
- lib/forwardable.rb
|
58
22
|
- lib/forwardable/impl.rb
|
23
|
+
- lib/forwardable/version.rb
|
59
24
|
homepage: https://github.com/ruby/forwardable
|
60
25
|
licenses:
|
61
26
|
- BSD-2-Clause
|
@@ -75,8 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
40
|
- !ruby/object:Gem::Version
|
76
41
|
version: '0'
|
77
42
|
requirements: []
|
78
|
-
|
79
|
-
rubygems_version: 2.7.6
|
43
|
+
rubygems_version: 3.0.3
|
80
44
|
signing_key:
|
81
45
|
specification_version: 4
|
82
46
|
summary: Provides delegation of specified methods to a designated object.
|
data/.gitignore
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/LICENSE.txt
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
-
|
3
|
-
Redistribution and use in source and binary forms, with or without
|
4
|
-
modification, are permitted provided that the following conditions
|
5
|
-
are met:
|
6
|
-
1. Redistributions of source code must retain the above copyright
|
7
|
-
notice, this list of conditions and the following disclaimer.
|
8
|
-
2. Redistributions in binary form must reproduce the above copyright
|
9
|
-
notice, this list of conditions and the following disclaimer in the
|
10
|
-
documentation and/or other materials provided with the distribution.
|
11
|
-
|
12
|
-
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
-
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
-
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
-
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
-
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
-
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
-
SUCH DAMAGE.
|
data/README.md
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
# Forwardable
|
2
|
-
|
3
|
-
The Forwardable module provides delegation of specified methods to a designated object, using the methods #def_delegator and #def_delegators.
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
Add this line to your application's Gemfile:
|
8
|
-
|
9
|
-
```ruby
|
10
|
-
gem 'forwardable'
|
11
|
-
```
|
12
|
-
|
13
|
-
And then execute:
|
14
|
-
|
15
|
-
$ bundle
|
16
|
-
|
17
|
-
Or install it yourself as:
|
18
|
-
|
19
|
-
$ gem install forwardable
|
20
|
-
|
21
|
-
## Usage
|
22
|
-
|
23
|
-
For example, say you have a class RecordCollection which contains an array <tt>@records</tt>. You could provide the lookup ethod `#record_number()`, which simply calls #[] on the <tt>@records</tt> array, like this:
|
24
|
-
|
25
|
-
```
|
26
|
-
require 'forwardable'
|
27
|
-
|
28
|
-
class RecordCollection
|
29
|
-
attr_accessor :records
|
30
|
-
extend Forwardable
|
31
|
-
def_delegator :@records, :[], :record_number
|
32
|
-
end
|
33
|
-
```
|
34
|
-
|
35
|
-
We can use the lookup method like so:
|
36
|
-
|
37
|
-
```
|
38
|
-
r = RecordCollection.new
|
39
|
-
r.records = [4,5,6]
|
40
|
-
r.record_number(0) # => 4
|
41
|
-
```
|
42
|
-
|
43
|
-
Further, if you wish to provide the methods #size, #<<, and #map, all of which delegate to @records, this is how you can do it:
|
44
|
-
|
45
|
-
```
|
46
|
-
class RecordCollection # re-open RecordCollection class
|
47
|
-
def_delegators :@records, :size, :<<, :map
|
48
|
-
end
|
49
|
-
|
50
|
-
r = RecordCollection.new
|
51
|
-
r.records = [1,2,3]
|
52
|
-
r.record_number(0) # => 1
|
53
|
-
r.size # => 3
|
54
|
-
r << 4 # => [1, 2, 3, 4]
|
55
|
-
r.map { |x| x * 2 } # => [2, 4, 6, 8]
|
56
|
-
```
|
57
|
-
|
58
|
-
You can even extend regular objects with Forwardable.
|
59
|
-
|
60
|
-
```
|
61
|
-
my_hash = Hash.new
|
62
|
-
my_hash.extend Forwardable # prepare object for delegation
|
63
|
-
my_hash.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
64
|
-
my_hash.puts "Howdy!"
|
65
|
-
```
|
66
|
-
|
67
|
-
## Development
|
68
|
-
|
69
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
70
|
-
|
71
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
72
|
-
|
73
|
-
## Contributing
|
74
|
-
|
75
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/forwardable.
|
76
|
-
|
77
|
-
## License
|
78
|
-
|
79
|
-
The gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause).
|
data/Rakefile
DELETED
data/bin/console
DELETED