class WithEvents::Invoker

Attributes

callable[R]

Public Class Methods

new(callable) click to toggle source
# File lib/with_events/invoker.rb, line 5
def initialize(callable)
  @callable = callable
end

Public Instance Methods

invoke(context, *args) click to toggle source
# File lib/with_events/invoker.rb, line 9
def invoke(context, *args)
  return context.instance_exec(*args, &callable) if proc?
  return callable.new.call(context, *args) if class?
  return context.public_send(callable, *args) if symbol?(context)

  raise NotImplementedError, 'Argument can not be invoked'
end

Private Instance Methods

class?() click to toggle source
# File lib/with_events/invoker.rb, line 25
def class?
  callable.is_a?(Class) && callable.instance_methods.include?(:call)
end
proc?() click to toggle source
# File lib/with_events/invoker.rb, line 21
def proc?
  callable.is_a?(Proc)
end
symbol?(context) click to toggle source
# File lib/with_events/invoker.rb, line 29
def symbol?(context)
  callable.is_a?(Symbol) && context.respond_to?(callable)
end