forward_to 0.3.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '079c056657ce3bbdeba3086c185a38723c13ef0b357780a56ad5839e249f1ffe'
4
- data.tar.gz: bc76638c1b0d7d480630d764c17078f4588684c1d00a0c9a820e6c9dfa0a4b59
3
+ metadata.gz: 240033d727e8213be920785aed1fc52d54f7620ec72dd4a197571c2dbc06e0d6
4
+ data.tar.gz: 58ded855c97316cd83e5c130b5f88f0950c897ee853ee57f574995b8d78d33f9
5
5
  SHA512:
6
- metadata.gz: c9b47190963da620d6da24fbef310fa40b9b522da5df19967e363f4a891f3f8db8d8253fd56172a424af9f1cbd0aad7a3c68ed03ebda4fe8e9591250deb56d71
7
- data.tar.gz: 782a9d3e395a922e4ada9352095384ec3b97fc2aa30223a59eabe10e4023719c943654131949387b4565ea1b59a4ec48683d06e7c348c995b4e1bf2b741b12a0
6
+ metadata.gz: 30b06925e6ca356d175a205efa1b3be2456c991a99b115b6abc8ea07e3c0fff53d0b2b1c3f7452694d2641f33ae61df16f1efcf9bacbc94540b71caa1056f330
7
+ data.tar.gz: b21168019876043beb494a8cddaf5791f0db4b2496b155d57f42c3530a8a20b78c2e7022fec913e67e2c144e1dea748a414c0d35df2c243205ca9fc7e75ea938
data/TODO ADDED
@@ -0,0 +1,12 @@
1
+
2
+ o Forbid String arguments. Strings make any Ruby expression legal with is not
3
+ what we wan't
4
+
5
+ + Find a way to forward to class methods
6
+ forward_to_class Module, :module_f
7
+ forward_to_class_method :class_method, :class_method_f
8
+
9
+ forward_to_class Prick, :settings
10
+ forward_to_class :settings, :super_conn, :user_conn, :conn
11
+
12
+
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ForwardTo
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/forward_to.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require_relative "forward_to/version"
4
4
 
5
5
  module ForwardTo
6
- # Forward methods to member object. The arguments should be strings or symbols
6
+ # Forward methods to member object, arguments can be strings or symbols
7
7
  #
8
8
  # Forward to takes the first argument and creates methods for the rest of the
9
9
  # arguments that forward their call to the first argument. Example
@@ -13,8 +13,8 @@ module ForwardTo
13
13
  # class MyArray
14
14
  # forward_to :@implementation, :empty?, :size, :[]
15
15
  #
16
- # def initialize()
17
- # @implementation = []
16
+ # def initialize(a = [])
17
+ # @implementation = a
18
18
  # end
19
19
  # end
20
20
  #
@@ -23,19 +23,67 @@ module ForwardTo
23
23
  # a.empty? # -> true
24
24
  # a[0] = 1
25
25
  # a[0] # -> 1
26
+ # a.implementation # Error - see attr_forward
26
27
  #
27
- # The target of #forward_to can be a method or a member (@variable) but has
28
- # to be a symbol
28
+ # The target of #forward_to can be an attribute or a member variable
29
29
  #
30
- def forward_to(target, *methods)
30
+ def forward_to(target, *methods) = impl_forward_to(target, methods, class_method: false)
31
+
32
+ # :call-seq:
33
+ # forward_to_class(class, *methods)
34
+ # forward_to_class(class-method, *methods)
35
+ #
36
+ # Create class methods that forwards to class or to the given class method:
37
+ #
38
+ # module Mod
39
+ # def self.module_method() = "other"
40
+ # end
41
+ #
42
+ # class Klass
43
+ # def self.class_method() = "string"
44
+ # forward_to_class :class_method, :size
45
+ # forward_to_class Mod, :module_method
46
+ # end
47
+ #
48
+ # Klass.size # -> 5
49
+ # Klass.module_method # -> "other"
50
+ #
51
+ def forward_to_class(target, *methods) = impl_forward_to(target.to_s, methods, class_method: true)
52
+
53
+
54
+ # Like #forward_to but also add an attr reader method for the member object:
55
+ #
56
+ # include ForwardTo
57
+ #
58
+ # class MyArray
59
+ # attr_forward :implementation, :empty?, :size, :[]
60
+ #
61
+ # def initialize(a = [])
62
+ # @implementation = a
63
+ # end
64
+ # end
65
+ #
66
+ # a = MyArray.new([1, 2, 3])
67
+ # a.implementation # -> [1, 2, 3]
68
+ #
69
+ # The target of #attr_reader should be an attribute
70
+ #
71
+ def attr_forward(target, *methods)
72
+ class_eval("attr_reader :#{target}")
73
+ forward_to(target, *methods)
74
+ end
75
+
76
+ private
77
+ def impl_forward_to(target, *methods, class_method: false)
78
+ qual = class_method ? "self." : ""
31
79
  for method in Array(methods).flatten
32
80
  case method
33
81
  when /\[\]=/
34
- class_eval("def #{method}(*args) #{target}&.#{method}(*args) end")
82
+ class_eval("def #{qual}#{method}(*args) #{target}&.#{method}(*args) end")
35
83
  when /=$/
36
- class_eval("def #{method}(args) #{target}&.#{method}(args) end")
84
+ class_eval("def #{qual}#{method}(args) #{target}&.#{method}(args) end")
37
85
  else
38
- class_eval("def #{method}(*args, &block) #{target}&.#{method}(*args, &block) end")
86
+ class_eval("def #{qual}#{method}(*args, &block) #{target}&.#{method}(*args, &block) end")
39
87
  end
40
88
  end
41
89
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forward_to
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2022-09-03 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Simple forwarding of methods
14
13
  email:
@@ -22,6 +21,7 @@ files:
22
21
  - Gemfile
23
22
  - README.md
24
23
  - Rakefile
24
+ - TODO
25
25
  - bin/console
26
26
  - bin/setup
27
27
  - forward_to.gemspec
@@ -32,7 +32,6 @@ licenses: []
32
32
  metadata:
33
33
  homepage_uri: https://github.com/clrgit/forward_to
34
34
  source_code_uri: https://github.com/clrgit/forward_to.git
35
- post_install_message:
36
35
  rdoc_options: []
37
36
  require_paths:
38
37
  - lib
@@ -47,8 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
46
  - !ruby/object:Gem::Version
48
47
  version: '0'
49
48
  requirements: []
50
- rubygems_version: 3.3.18
51
- signing_key:
49
+ rubygems_version: 3.6.9
52
50
  specification_version: 4
53
51
  summary: Simple forwarding of methods
54
52
  test_files: []