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



29
30
31
32
33
34
35
36
# File 'lib/statecraft/machine.rb', line 29

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



38
39
40
# File 'lib/statecraft/machine.rb', line 38

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

.unary?(callable) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/statecraft/machine.rb', line 42

def self.unary?(callable)
  callable.arity == 1
end