rubysl-forwardable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE +25 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/forwardable.rb +1 -0
- data/lib/rubysl/forwardable.rb +2 -0
- data/lib/rubysl/forwardable/forwardable.rb +218 -0
- data/lib/rubysl/forwardable/version.rb +5 -0
- data/rubysl-forwardable.gemspec +23 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f8dc4c1445a7bc908a8a5082265c1222e8474f5f
|
4
|
+
data.tar.gz: c3ad9fcd21418c1fca9fe5ff09d91bbd35445af8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 672b1e711ad4bb718704a43f7fca3523615407642803d438366ca87899f98e697ed8c5fa7fa3155d4bb89253b60bd432ab4464d71d75ba039391745df859cb8b
|
7
|
+
data.tar.gz: 209a0f12c8bd8777d78399703219d01bef83f9ea472faf0449fefaed0268b744b8e481ed2e124caeecea26d51b9159485f15c74b3c28114ce981727e4359e11e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (c) 2013, Brian Shirai
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
3. Neither the name of the library nor the names of its contributors may be
|
13
|
+
used to endorse or promote products derived from this software without
|
14
|
+
specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
|
20
|
+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
21
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
22
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
23
|
+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
24
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
25
|
+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Rubysl::Forwardable
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rubysl-forwardable'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rubysl-forwardable
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/forwardable.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "rubysl/forwardable"
|
@@ -0,0 +1,218 @@
|
|
1
|
+
# = forwardable - Support for the Delegation Pattern
|
2
|
+
#
|
3
|
+
# $Release Version: 1.1$
|
4
|
+
# $Revision: 11708 $
|
5
|
+
# $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
|
6
|
+
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
7
|
+
#
|
8
|
+
# Documentation by James Edward Gray II and Gavin Sinclair
|
9
|
+
#
|
10
|
+
# == Introduction
|
11
|
+
#
|
12
|
+
# This library allows you delegate method calls to an object, on a method by
|
13
|
+
# method basis. You can use Forwardable to setup this delegation at the class
|
14
|
+
# level, or SingleForwardable to handle it at the object level.
|
15
|
+
#
|
16
|
+
# == Notes
|
17
|
+
#
|
18
|
+
# Be advised, RDoc will not detect delegated methods.
|
19
|
+
#
|
20
|
+
# <b>forwardable.rb provides single-method delegation via the
|
21
|
+
# def_delegator() and def_delegators() methods. For full-class
|
22
|
+
# delegation via DelegateClass(), see delegate.rb.</b>
|
23
|
+
#
|
24
|
+
# == Examples
|
25
|
+
#
|
26
|
+
# === Forwardable
|
27
|
+
#
|
28
|
+
# Forwardable makes building a new class based on existing work, with a proper
|
29
|
+
# interface, almost trivial. We want to rely on what has come before obviously,
|
30
|
+
# but with delegation we can take just the methods we need and even rename them
|
31
|
+
# as appropriate. In many cases this is preferable to inheritance, which gives
|
32
|
+
# us the entire old interface, even if much of it isn't needed.
|
33
|
+
#
|
34
|
+
# class Queue
|
35
|
+
# extend Forwardable
|
36
|
+
#
|
37
|
+
# def initialize
|
38
|
+
# @q = [ ] # prepare delegate object
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# # setup prefered interface, enq() and deq()...
|
42
|
+
# def_delegator :@q, :push, :enq
|
43
|
+
# def_delegator :@q, :shift, :deq
|
44
|
+
#
|
45
|
+
# # support some general Array methods that fit Queues well
|
46
|
+
# def_delegators :@q, :clear, :first, :push, :shift, :size
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# q = Queue.new
|
50
|
+
# q.enq 1, 2, 3, 4, 5
|
51
|
+
# q.push 6
|
52
|
+
#
|
53
|
+
# q.shift # => 1
|
54
|
+
# while q.size > 0
|
55
|
+
# puts q.deq
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# q.enq "Ruby", "Perl", "Python"
|
59
|
+
# puts q.first
|
60
|
+
# q.clear
|
61
|
+
# puts q.first
|
62
|
+
#
|
63
|
+
# <i>Prints:</i>
|
64
|
+
#
|
65
|
+
# 2
|
66
|
+
# 3
|
67
|
+
# 4
|
68
|
+
# 5
|
69
|
+
# 6
|
70
|
+
# Ruby
|
71
|
+
# nil
|
72
|
+
#
|
73
|
+
# === SingleForwardable
|
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
|
+
# <i>Prints:</i>
|
81
|
+
#
|
82
|
+
# Howdy!
|
83
|
+
|
84
|
+
#
|
85
|
+
# The Forwardable module provides delegation of specified
|
86
|
+
# methods to a designated object, using the methods #def_delegator
|
87
|
+
# and #def_delegators.
|
88
|
+
#
|
89
|
+
# For example, say you have a class RecordCollection which
|
90
|
+
# contains an array <tt>@records</tt>. You could provide the lookup method
|
91
|
+
# #record_number(), which simply calls #[] on the <tt>@records</tt>
|
92
|
+
# array, like this:
|
93
|
+
#
|
94
|
+
# class RecordCollection
|
95
|
+
# extend Forwardable
|
96
|
+
# def_delegator :@records, :[], :record_number
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# Further, if you wish to provide the methods #size, #<<, and #map,
|
100
|
+
# all of which delegate to @records, this is how you can do it:
|
101
|
+
#
|
102
|
+
# class RecordCollection
|
103
|
+
# # extend Forwardable, but we did that above
|
104
|
+
# def_delegators :@records, :size, :<<, :map
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
# Also see the example at forwardable.rb.
|
108
|
+
#
|
109
|
+
module Forwardable
|
110
|
+
|
111
|
+
@debug = nil
|
112
|
+
class<<self
|
113
|
+
# force Forwardable to show up in stack backtraces of delegated methods
|
114
|
+
attr_accessor :debug
|
115
|
+
end
|
116
|
+
|
117
|
+
#
|
118
|
+
# Shortcut for defining multiple delegator methods, but with no
|
119
|
+
# provision for using a different name. The following two code
|
120
|
+
# samples have the same effect:
|
121
|
+
#
|
122
|
+
# def_delegators :@records, :size, :<<, :map
|
123
|
+
#
|
124
|
+
# def_delegator :@records, :size
|
125
|
+
# def_delegator :@records, :<<
|
126
|
+
# def_delegator :@records, :map
|
127
|
+
#
|
128
|
+
# See the examples at Forwardable and forwardable.rb.
|
129
|
+
#
|
130
|
+
def def_instance_delegators(accessor, *methods)
|
131
|
+
for method in methods
|
132
|
+
def_instance_delegator(accessor, method)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
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
|
+
def def_instance_delegator(accessor, method, ali = method)
|
144
|
+
accessor = accessor.id2name if accessor.kind_of?(Integer)
|
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)
|
149
|
+
def #{ali}(*args, &block)
|
150
|
+
begin
|
151
|
+
#{accessor}.__send__(:#{method}, *args, &block)
|
152
|
+
rescue Exception
|
153
|
+
$@.delete_if{|s| /^\\(__FORWARDABLE__\\):/ =~ s} unless Forwardable::debug
|
154
|
+
Kernel::raise
|
155
|
+
end
|
156
|
+
end
|
157
|
+
EOS
|
158
|
+
end
|
159
|
+
|
160
|
+
alias def_delegators def_instance_delegators
|
161
|
+
alias def_delegator def_instance_delegator
|
162
|
+
end
|
163
|
+
|
164
|
+
#
|
165
|
+
# The SingleForwardable module provides delegation of specified
|
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.
|
171
|
+
#
|
172
|
+
module SingleForwardable
|
173
|
+
#
|
174
|
+
# Shortcut for defining multiple delegator methods, but with no
|
175
|
+
# provision for using a different name. The following two code
|
176
|
+
# samples have the same effect:
|
177
|
+
#
|
178
|
+
# single_forwardable.def_delegators :@records, :size, :<<, :map
|
179
|
+
#
|
180
|
+
# single_forwardable.def_delegator :@records, :size
|
181
|
+
# single_forwardable.def_delegator :@records, :<<
|
182
|
+
# single_forwardable.def_delegator :@records, :map
|
183
|
+
#
|
184
|
+
# See the example at forwardable.rb.
|
185
|
+
#
|
186
|
+
def def_singleton_delegators(accessor, *methods)
|
187
|
+
for method in methods
|
188
|
+
def_singleton_delegator(accessor, method)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
#
|
193
|
+
# Defines a method _method_ which delegates to _obj_ (i.e. it calls
|
194
|
+
# the method of the same name in _obj_). If _new_name_ is
|
195
|
+
# provided, it is used as the name for the delegate method.
|
196
|
+
#
|
197
|
+
# See the example at forwardable.rb.
|
198
|
+
#
|
199
|
+
def def_singleton_delegator(accessor, method, ali = method)
|
200
|
+
accessor = accessor.id2name if accessor.kind_of?(Integer)
|
201
|
+
method = method.id2name if method.kind_of?(Integer)
|
202
|
+
ali = ali.id2name if ali.kind_of?(Integer)
|
203
|
+
|
204
|
+
instance_eval(<<-EOS, "(__FORWARDABLE__)", 1)
|
205
|
+
def #{ali}(*args, &block)
|
206
|
+
begin
|
207
|
+
#{accessor}.__send__(:#{method}, *args,&block)
|
208
|
+
rescue Exception
|
209
|
+
$@.delete_if{|s| /^\\(__FORWARDABLE__\\):/ =~ s} unless Forwardable::debug
|
210
|
+
Kernel::raise
|
211
|
+
end
|
212
|
+
end
|
213
|
+
EOS
|
214
|
+
end
|
215
|
+
|
216
|
+
alias def_delegators def_singleton_delegators
|
217
|
+
alias def_delegator def_singleton_delegator
|
218
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require './lib/rubysl/forwardable/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "rubysl-forwardable"
|
6
|
+
spec.version = RubySL::Forwardable::VERSION
|
7
|
+
spec.authors = ["Brian Shirai"]
|
8
|
+
spec.email = ["brixen@gmail.com"]
|
9
|
+
spec.description = %q{Ruby standard library forwardable.}
|
10
|
+
spec.summary = %q{Ruby standard library forwardable.}
|
11
|
+
spec.homepage = "https://github.com/rubysl/rubysl-forwardable"
|
12
|
+
spec.license = "BSD"
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "mspec", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubysl-forwardable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Shirai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-25 00:00:00.000000000 Z
|
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: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
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
|
+
description: Ruby standard library forwardable.
|
70
|
+
email:
|
71
|
+
- brixen@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .travis.yml
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/forwardable.rb
|
83
|
+
- lib/rubysl/forwardable.rb
|
84
|
+
- lib/rubysl/forwardable/forwardable.rb
|
85
|
+
- lib/rubysl/forwardable/version.rb
|
86
|
+
- rubysl-forwardable.gemspec
|
87
|
+
homepage: https://github.com/rubysl/rubysl-forwardable
|
88
|
+
licenses:
|
89
|
+
- BSD
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.0.7
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Ruby standard library forwardable.
|
111
|
+
test_files: []
|