forward_to 0.4.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: b28ac6a51db0a89c6c59a4a4eff5a72806bfd4103702c6e0416c2aabdab9d20c
4
- data.tar.gz: cf0f8c489d4d30e8e7c05373af3551af1f52c984dcc664ff9ddbf28aa828bd17
3
+ metadata.gz: 240033d727e8213be920785aed1fc52d54f7620ec72dd4a197571c2dbc06e0d6
4
+ data.tar.gz: 58ded855c97316cd83e5c130b5f88f0950c897ee853ee57f574995b8d78d33f9
5
5
  SHA512:
6
- metadata.gz: 19a767c6d285fddc05186e16c769eb9d09ffb6dfceceb55d6956e78583a1209822cce576568d45666e595043a9ac9beecf0e98c1ebb4d1e580d23631c72e187c
7
- data.tar.gz: 71429d20828ae7b7c12f4eb4bba99b39637418db10f334308690a03fdd06660f4d7b4e192f6c81471263515e5dfa855de147645540f4a5adaeeab1cf6de49f53
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.4.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
@@ -23,23 +23,35 @@ 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
28
  # The target of #forward_to can be an attribute or a member variable
28
29
  #
29
- def forward_to(target, *methods)
30
- for method in Array(methods).flatten
31
- case method
32
- when /\[\]=/
33
- class_eval("def #{method}(*args) #{target}&.#{method}(*args) end")
34
- when /=$/
35
- class_eval("def #{method}(args) #{target}&.#{method}(args) end")
36
- else
37
- class_eval("def #{method}(*args, &block) #{target}&.#{method}(*args, &block) end")
38
- end
39
- end
40
- end
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
+
41
53
 
42
- # List #forward_to but also an attr reader method for the member object:
54
+ # Like #forward_to but also add an attr reader method for the member object:
43
55
  #
44
56
  # include ForwardTo
45
57
  #
@@ -60,5 +72,20 @@ module ForwardTo
60
72
  class_eval("attr_reader :#{target}")
61
73
  forward_to(target, *methods)
62
74
  end
75
+
76
+ private
77
+ def impl_forward_to(target, *methods, class_method: false)
78
+ qual = class_method ? "self." : ""
79
+ for method in Array(methods).flatten
80
+ case method
81
+ when /\[\]=/
82
+ class_eval("def #{qual}#{method}(*args) #{target}&.#{method}(*args) end")
83
+ when /=$/
84
+ class_eval("def #{qual}#{method}(args) #{target}&.#{method}(args) end")
85
+ else
86
+ class_eval("def #{qual}#{method}(*args, &block) #{target}&.#{method}(*args, &block) end")
87
+ end
88
+ end
89
+ end
63
90
  end
64
91
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forward_to
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
@@ -21,6 +21,7 @@ files:
21
21
  - Gemfile
22
22
  - README.md
23
23
  - Rakefile
24
+ - TODO
24
25
  - bin/console
25
26
  - bin/setup
26
27
  - forward_to.gemspec