class Casting::Delegation

Attributes

arguments[RW]
attendant[RW]
block[RW]
client[RW]
delegated_method_name[RW]

Public Class Methods

new(**settings, &block) click to toggle source
# File lib/casting/delegation.rb, line 18
def initialize(**settings, &block)
  @delegated_method_name = settings[:delegated_method_name]
  @client = settings[:client]
  @attendant = settings[:attendant]
  @arguments = settings[:arguments]
  @keyword_arguments = settings[:keyword_arguments]
  @block = block
end
prepare(delegated_method_name, client, &block) click to toggle source
# File lib/casting/delegation.rb, line 11
def self.prepare(delegated_method_name, client, &block)
  new(delegated_method_name: delegated_method_name, client: client, &block)
end

Public Instance Methods

call(*args, **kwargs, &block) click to toggle source
# File lib/casting/delegation.rb, line 44
def call(*args, **kwargs, &block)
  raise MissingAttendant.new unless attendant

  call_args = positional_arguments(args)
  call_kwargs = keyword_arguments(kwargs)
  call_block = block_argument(&block)

  case
  when call_args && call_kwargs
    bound_method.call(*call_args, **call_kwargs, &call_block)
  when call_args
    bound_method.call(*call_args, &call_block)
  when call_kwargs
    bound_method.call(**call_kwargs, &call_block)
  else
    bound_method.call(&call_block)
  end
end
to(object_or_module) click to toggle source
# File lib/casting/delegation.rb, line 27
def to(object_or_module)
  @attendant = object_or_module
  begin
    bound_method
  rescue TypeError
    @attendant = method_module || raise
  end
  self
end
with(*args, **kwargs, &block) click to toggle source
# File lib/casting/delegation.rb, line 37
def with(*args, **kwargs, &block)
  @arguments = args
  @keyword_arguments = kwargs
  @block = block
  self
end

Private Instance Methods

block_argument(&block) click to toggle source
# File lib/casting/delegation.rb, line 65
def block_argument(&block)
  block || @block
end
bound_method() click to toggle source
# File lib/casting/delegation.rb, line 79
def bound_method
  delegated_method.bind(client)
rescue TypeError
  raise TypeError.new("`to' argument must be a module or an object with #{delegated_method_name} defined in a module")
end
delegated_method() click to toggle source
# File lib/casting/delegation.rb, line 92
def delegated_method
  if Module === attendant
    attendant
  else
    attendant.method(delegated_method_name).owner
  end.instance_method(delegated_method_name)
rescue NameError => e
  raise InvalidAttendant, e.message
end
keyword_arguments(options) click to toggle source
# File lib/casting/delegation.rb, line 74
def keyword_arguments(options)
  return options unless options.empty?
  @keyword_arguments
end
method_module() click to toggle source
# File lib/casting/delegation.rb, line 85
def method_module
  mod = delegated_method.owner
  unless mod.is_a?(Class)
    mod
  end
end
positional_arguments(options) click to toggle source
# File lib/casting/delegation.rb, line 69
def positional_arguments(options)
  return options unless options.empty?
  @arguments
end