Module: DelegateVia::ClassMethods
- Defined in:
- lib/pad/delegate_via.rb
Instance Method Summary collapse
-
#delegate_via(accessor_name, *method_names, &return_block) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
Instance Method Details
#delegate_via(accessor_name, *method_names, &return_block) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pad/delegate_via.rb', line 11 def delegate_via(accessor_name, *method_names, &return_block) = method_names.last.is_a?(Hash) ? method_names.pop : {} via_method = .fetch(:via, :map) mod = if const_defined?(:DelegateVia, false) const_get(:DelegateVia) else new_mod = Module.new do def self.to_s "DelegateVia(#{instance_methods(false).join(', ')})" end end const_set(:DelegateVia, new_mod) end mod.module_eval do method_names.each do |method_name| define_method method_name do |*args, &block| accessor = __send__(accessor_name) result = accessor.__send__(via_method) { |service| service.__send__(method_name, *args, &block) } return_block ? instance_exec(result, &return_block) : result end end end include mod end |