Module: SleepingKingStudios::Tools::Toolbox::Delegator
- Defined in:
- lib/sleeping_king_studios/tools/toolbox/delegator.rb
Overview
Module for extending classes with basic delegation. Supports passing arguments, keywords, and blocks to the delegated method.
Class Method Summary collapse
Instance Method Summary collapse
-
#delegate(*method_names, to: nil, allow_nil: false) ⇒ Object
Defines a wrapper method to delegate implementation of the specified method or methods to an object, to the object at another specified method, or to the object at a specified instance variable.
-
#wrap_delegate(target, klass: nil, except: [], only: []) ⇒ Object
Wraps a delegate object by automatically delegating each method that is defined on the delegate class from the instance to the delegate.
Class Method Details
.extended(_module) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/sleeping_king_studios/tools/toolbox/delegator.rb', line 21 def self.extended(_module) super SleepingKingStudios::Tools::CoreTools.deprecate( 'SleepingKingStudios::Tools::Toolbox::Delegator', message: 'Use Ruby stdlib Forwardable instead.' ) end |
Instance Method Details
#delegate(*method_names, to: nil, allow_nil: false) ⇒ Object
Defines a wrapper method to delegate implementation of the specified method or methods to an object, to the object at another specified method, or to the object at a specified instance variable.
75 76 77 78 79 80 81 |
# File 'lib/sleeping_king_studios/tools/toolbox/delegator.rb', line 75 def delegate(*method_names, to: nil, allow_nil: false) raise ArgumentError, 'must specify a delegate' if to.nil? && !allow_nil method_names.each do |method_name| delegate_method method_name, to, { allow_nil: !!allow_nil } end end |
#wrap_delegate(target, klass: nil, except: [], only: []) ⇒ Object
Wraps a delegate object by automatically delegating each method that is defined on the delegate class from the instance to the delegate. The delegate can be specified with an object literal or with the name of an instance method or instance variable.
Only methods that are defined at the time #wrap_delegate is called will be delegated, so make sure to call #wrap_delegate after loading any gems or libraries that extend your delegate class, such as ActiveSupport.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/sleeping_king_studios/tools/toolbox/delegator.rb', line 136 def wrap_delegate(target, klass: nil, except: [], only: []) if klass.is_a?(Module) unless target.is_a?(String) || target.is_a?(Symbol) || target.is_a?(klass) raise ArgumentError, "expected delegate to be a #{klass.name}" end method_names = klass.instance_methods - Object.instance_methods elsif target.is_a?(String) || target.is_a?(Symbol) raise ArgumentError, 'must specify a delegate class' else method_names = target.methods - Object.new.methods end if except.is_a?(Array) && !except.empty? method_names -= except.map(&:intern) end method_names &= only.map(&:intern) if only.is_a?(Array) && !only.empty? delegate(*method_names, to: target) end |