Module: ForwardableX
- Included in:
- Object
- Defined in:
- lib/forwardablex.rb,
lib/forwardablex/version.rb
Overview
ForwardableX is a module for providing extra Forwardable functions. Benefits to use this library are the following:
-
you can use easy keyword “forward”, instead of “def_instance_delegator” or “def_delegator”
-
you can specify message receiver as instance variable name, Proc object or plain object
-
no need to declare “extend Forwardable”
-
forwardable.rb compatible API
Constant Summary collapse
- VERSION =
version of ForwardableX
"0.1.4"
Instance Method Summary collapse
-
#delegate(hash) ⇒ void
Same as Forwardable#delegate, but you can specify receivers as instance variable name, Proc object, and plain object.
-
#forward(receiver, method, name = method) ⇒ void
(also: #def_instance_delegator, #def_singleton_delegator, #def_delegator)
Define a method that forwards the message to the receiver.
-
#forward!(receiver, *methods) ⇒ void
(also: #def_instance_delegators, #def_singleton_delegators, #def_delegators)
Define each method that calls the receiver’s method.
-
#forward_as_key(receiver, key, name = key) ⇒ void
Define a method that forwards the key to the receiver.
-
#forward_as_key!(receiver, *keys) ⇒ void
Define each method that forwards to the receiver as key.
Instance Method Details
#delegate(hash) ⇒ void
This method returns an undefined value.
Same as Forwardable#delegate, but you can specify receivers as instance variable name, Proc object, and plain object.
98 99 100 101 102 |
# File 'lib/forwardablex.rb', line 98 def delegate(hash) hash.each do |methods, receiver| forward!(receiver, *methods) end end |
#forward(receiver, method, name = method) ⇒ void Also known as: def_instance_delegator, def_singleton_delegator, def_delegator
This method returns an undefined value.
Define a method that forwards the message to the receiver. You can specify receiver as instance variable name, Proc object, and plain object.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/forwardablex.rb', line 55 def forward(receiver, method, name=method) context = self.kind_of?(Module) ? self : self.singleton_class context.instance_eval do case receiver when :class forward_class_receiver(method, name) when :identity forward_identity_receiver(name) when Symbol, String forward_named_receiver(receiver, method, name) when Proc forward_proc_receiver(receiver, method, name) else forward_object_receiver(receiver, method, name) end end end |
#forward!(receiver, *methods) ⇒ void Also known as: def_instance_delegators, def_singleton_delegators, def_delegators
This method returns an undefined value.
Define each method that calls the receiver’s method.
83 84 85 86 87 |
# File 'lib/forwardablex.rb', line 83 def forward!(receiver, *methods) methods.delete("__send__") methods.delete("__id__") methods.each {|method| forward(receiver, method)} end |
#forward_as_key(receiver, key, name = key) ⇒ void
This method returns an undefined value.
Define a method that forwards the key to the receiver. You can specify receiver as instance variable name, Proc object, and plain object.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/forwardablex.rb', line 114 def forward_as_key(receiver, key, name=key) context = self.kind_of?(Module) ? self : self.singleton_class context.instance_eval do case receiver when :class forward_class_receiver(:[], name, key) when :identity forward_identity_receiver(name) when Symbol, String forward_named_receiver(receiver, :[], name, key) when Proc forward_proc_receiver(receiver, :[], name, key) else forward_object_receiver(receiver, :[], name, key) end end end |
#forward_as_key!(receiver, *keys) ⇒ void
This method returns an undefined value.
Define each method that forwards to the receiver as key.
139 140 141 |
# File 'lib/forwardablex.rb', line 139 def forward_as_key!(receiver, *keys) keys.each {|key| forward_as_key(receiver, key)} end |