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



38
39
40
41
42
43
44
45
# File 'lib/statecraft/machine.rb', line 38

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



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

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

.unary?(callable) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/statecraft/machine.rb', line 51

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