forward_to 0.1.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 +7 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/Gemfile +10 -0
- data/README.md +51 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/forward_to.gemspec +34 -0
- data/lib/forward_to/version.rb +5 -0
- data/lib/forward_to.rb +39 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e324925982b40a9a54c191ae4277dd4e89dc773ff66203d3a109dc50aa8d409e
|
4
|
+
data.tar.gz: ee351092ac334d8f496dde7385914e90e5ea65831ceb390e169925997ad571e9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d4badf84c162ee10a8e41fde02a904d8935ca86b6e37dd0859068601c43a90a1d4d9401ad22c68fb9a93846e9a691f5defc7b86d345bd7a5a195692643d7f33b
|
7
|
+
data.tar.gz: 285ad8105252e9b1688c8eb30f6c88e48189b4e75a9c04bf01ee09f75eafd8bbea1a06f4bd3253cd8a73cb460a4b51bdc67a2fe3fa2bb05e6abe0f2275ef8bfc
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.7.1
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# ForwardTo
|
2
|
+
|
3
|
+
ForwardTo provides a #forward_to method that can be used to forward methods to
|
4
|
+
a member object. It resembels the rails #delegate method but with a different
|
5
|
+
syntax
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'forward_to'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install forward_to
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
You will typically include the ForwardTo module globally to have #forward_to
|
26
|
+
available everywhere:
|
27
|
+
|
28
|
+
require 'forward_to'
|
29
|
+
|
30
|
+
include ForwardTo
|
31
|
+
|
32
|
+
class A
|
33
|
+
forward_to :@implementation, :size, :[], :[]=
|
34
|
+
def initialize() @implementation = [] end
|
35
|
+
end
|
36
|
+
|
37
|
+
The first argument to #forward_to is the target object. It can be a member
|
38
|
+
method or an instance variable but in both cases it has to be specified as a
|
39
|
+
Symbol. The rest of the arguments are names of the member methods (Symbol) that
|
40
|
+
will be forwarded to the target. Using the definitions above it is possible to
|
41
|
+
do
|
42
|
+
|
43
|
+
a = A.new
|
44
|
+
puts a.size # => 0
|
45
|
+
|
46
|
+
a[0] = 1
|
47
|
+
puts a[0] # => 1
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/clrgit/forward_to.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "forward_to"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/forward_to.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/forward_to/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "forward_to"
|
7
|
+
spec.version = ForwardTo::VERSION
|
8
|
+
spec.authors = ["Claus Rasmussen"]
|
9
|
+
spec.email = ["ras@danak.dk"]
|
10
|
+
|
11
|
+
spec.summary = "Forward methods"
|
12
|
+
spec.description = "Forwards instance methods to a member of an instance variable. It works like Rails #delegate but with a different syntax"
|
13
|
+
spec.homepage = "http://github.com/clrgit/forward_to"
|
14
|
+
spec.required_ruby_version = ">= 2.4.0"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
22
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
23
|
+
end
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
# Uncomment to register a new dependency of your gem
|
30
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
31
|
+
|
32
|
+
# For more information and examples about making a new gem, checkout our
|
33
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
34
|
+
end
|
data/lib/forward_to.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "forward_to/version"
|
4
|
+
|
5
|
+
module ForwardTo
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
# Forward methods to member object. The arguments should be strings or symbols
|
9
|
+
#
|
10
|
+
# Forward to takes the first argument and creates methods for the rest of the
|
11
|
+
# arguments that forward their call to the first argument. Example
|
12
|
+
#
|
13
|
+
# include ForwardTo
|
14
|
+
#
|
15
|
+
# class MyArray
|
16
|
+
# forward_to :@implementation, :empty?, :size, :[]
|
17
|
+
#
|
18
|
+
# def initialize()
|
19
|
+
# @implementation = []
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# a = MyArray.new
|
24
|
+
# a.size # -> 0
|
25
|
+
#
|
26
|
+
# The target of #forward_to can be a method or a member (@variable) but has
|
27
|
+
# to be a symbol
|
28
|
+
#
|
29
|
+
def forward_to(target, *methods)
|
30
|
+
for method in Array(methods).flatten
|
31
|
+
if method =~ /=$/
|
32
|
+
class_eval("def #{method}(*args) #{target}&.#{method}(*args) end")
|
33
|
+
else
|
34
|
+
class_eval("def #{method}(*args, &block) #{target}&.#{method}(*args, &block) end")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: forward_to
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Claus Rasmussen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-09-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: 'Forwards instance methods to a member of an instance variable. It works
|
14
|
+
like Rails #delegate but with a different syntax'
|
15
|
+
email:
|
16
|
+
- ras@danak.dk
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".ruby-version"
|
23
|
+
- Gemfile
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- forward_to.gemspec
|
29
|
+
- lib/forward_to.rb
|
30
|
+
- lib/forward_to/version.rb
|
31
|
+
homepage: http://github.com/clrgit/forward_to
|
32
|
+
licenses: []
|
33
|
+
metadata:
|
34
|
+
homepage_uri: http://github.com/clrgit/forward_to
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.4.0
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubygems_version: 3.2.26
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: Forward methods
|
54
|
+
test_files: []
|