Class: BusinessFlow::Callable

Inherits:
Object
  • Object
show all
Defined in:
lib/business_flow/callable.rb

Overview

Isolate the logic around invoking a ‘callable’ – a symbol representing a method, a symbol representing another class which implements .call, or a proc/lambda

Instance Method Summary collapse

Constructor Details

#initialize(callable) ⇒ Callable

Returns a new instance of Callable.



8
9
10
11
# File 'lib/business_flow/callable.rb', line 8

def initialize(callable)
  @callable = callable
  check_callable
end

Instance Method Details

#call(instance, inputs) ⇒ Object

:reek:ManualDispatch At this stage @callable is a symbol and is either a method or a constant. Checking respond_to? is easier and faster than generating a NoMethodError and catching it.



16
17
18
19
20
21
22
23
24
25
# File 'lib/business_flow/callable.rb', line 16

def call(instance, inputs)
  if instance.respond_to?(@callable, true)
    send_callable(instance, inputs)
  else
    @callable = lookup_callable(instance) ||
                raise(NameError, "undefined constant #{@callable}")
    check_callable
    call(instance, inputs)
  end
end

#to_sObject



27
28
29
# File 'lib/business_flow/callable.rb', line 27

def to_s
  @callable.to_s
end