Module: Mixins::Delegation
- Defined in:
- lib/mixins/delegation.rb
Overview
A collection of various delegation code-generators that can be used to define delegation through other methods, to instance variables, etc.
Class Method Summary collapse
-
.make_ivar_delegator(ivar_name, name) ⇒ Object
Make the body of a delegator method that will delegate calls to the
namemethod to the givenivar. -
.make_method_delegator(delegate, name) ⇒ Object
Make the body of a delegator method that will delegate to the
namemethod of the object returned by thedelegatemethod.
Instance Method Summary collapse
-
#def_class_delegators(*delegated_methods) ⇒ Object
Define the given
delegated_methodsas delegators to the like-named class method. -
#def_ivar_delegators(ivar, *delegated_methods) ⇒ Object
Define the given
delegated_methodsas delegators to the like-named method of the specifiedivar. -
#def_method_delegators(delegate_method, *delegated_methods) ⇒ Object
Define the given
delegated_methodsas delegators to the like-named method of the return value of thedelegate_method.
Class Method Details
.make_ivar_delegator(ivar_name, name) ⇒ Object
Make the body of a delegator method that will delegate calls to the name method to the given ivar.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mixins/delegation.rb', line 84 def make_ivar_delegator( ivar_name, name ) return ->( *args, **kwargs, &block ) do ivar = self.instance_variable_get( ivar_name ) ivar.__send__( name, *args, **kwargs, &block ) rescue => err bt = err.backtrace_locations bt.shift raise( err, err., bt, cause: err ) end end |
.make_method_delegator(delegate, name) ⇒ Object
Make the body of a delegator method that will delegate to the name method of the object returned by the delegate method.
71 72 73 74 75 76 77 78 79 |
# File 'lib/mixins/delegation.rb', line 71 def make_method_delegator( delegate, name ) return ->( *args, **kwargs, &block ) do self.__send__( delegate ).__send__( name, *args, **kwargs, &block ) rescue => err bt = err.backtrace_locations bt.shift raise( err, err., bt, cause: err ) end end |
Instance Method Details
#def_class_delegators(*delegated_methods) ⇒ Object
Define the given delegated_methods as delegators to the like-named class method.
56 57 58 59 60 61 62 |
# File 'lib/mixins/delegation.rb', line 56 def def_class_delegators( *delegated_methods ) delegated_methods.each do |name| define_method( name ) do |*args| self.class.__send__( name, *args ) end end end |
#def_ivar_delegators(ivar, *delegated_methods) ⇒ Object
Define the given delegated_methods as delegators to the like-named method of the specified ivar. This is pretty much identical with how ‘Forwardable’ from the stdlib does delegation, but it’s reimplemented here for consistency.
class MyClass
extend Mixins::Delegation
# Delegate the #each method to the @collection ivar
def_ivar_delegators :@collection, :each
end
46 47 48 49 50 51 |
# File 'lib/mixins/delegation.rb', line 46 def def_ivar_delegators( ivar, *delegated_methods ) delegated_methods.each do |name| body = Mixins::Delegation.make_ivar_delegator( ivar, name ) define_method( name, &body ) end end |
#def_method_delegators(delegate_method, *delegated_methods) ⇒ Object
Define the given delegated_methods as delegators to the like-named method of the return value of the delegate_method.
class MyClass
extend Mixins::Delegation
# Delegate the #bound?, #err, and #result2error methods to the connection
# object returned by the #connection method. This allows the connection
# to still be loaded on demand/overridden/etc.
def_method_delegators :connection, :bound?, :err, :result2error
def connection
@connection ||= self.connect
end
end
26 27 28 29 30 31 |
# File 'lib/mixins/delegation.rb', line 26 def def_method_delegators( delegate_method, *delegated_methods ) delegated_methods.each do |name| body = Mixins::Delegation.make_method_delegator( delegate_method, name ) define_method( name, &body ) end end |