Module: Statecraft::Machine::Handlers

Defined in:
lib/statecraft/machine.rb

Overview

Invokes a guard or callback handler with the honest-call convention: a Symbol resolves to a (possibly private) instance method of the machine, a callable is called as-is with self untouched. Arity 1 receives the record alone, everything else receives (record, payload) — the same dispatch Active Record validators use.

Class Method Summary collapse

Class Method Details

.invoke(machine_instance, handler, record, payload) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/statecraft/machine.rb', line 35

def self.invoke(machine_instance, handler, record, payload)
  callable = resolve(machine_instance, handler)
  if unary?(callable)
    callable.call(record)
  else
    callable.call(record, payload)
  end
end

.resolve(machine_instance, handler) ⇒ Object



44
45
46
# File 'lib/statecraft/machine.rb', line 44

def self.resolve(machine_instance, handler)
  handler.is_a?(Symbol) ? machine_instance.method(handler) : handler
end

.unary?(callable) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/statecraft/machine.rb', line 48

def self.unary?(callable)
  arity = callable.respond_to?(:arity) ? callable.arity : callable.method(:call).arity
  arity == 1
end