Module: Statecraft::Introspection

Defined in:
lib/statecraft/introspection.rb

Overview

The record-facing questions: "would the guards pass if I fired this transition with these metadata right now". Normalization and freeze are identical to the pipeline's, so a guard that mutates metadata fails the same way in a check as in a transition, and the answer never diverges from what fire! would actually do. Every answer is a snapshot: CAS may still reject the transition later.

Defined Under Namespace

Classes: Availability

Instance Method Summary collapse

Instance Method Details

#available_events(metadata: {}) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/statecraft/introspection.rb', line 24

def available_events(metadata: {})
  normalized = .normalize()
  statecraft_graph.events.filter_map do |event_name, branches|
    edge = branches[statecraft_current_state]
    next unless edge

    event_name if statecraft_guards_pass?(edge, event_name, normalized)
  end
end

#available_transitions(metadata: {}) ⇒ Object



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

def available_transitions(metadata: {})
  normalized = .normalize()
  statecraft_graph.edges.filter_map do |(from, _to), edge|
    next unless from == statecraft_current_state

    via = statecraft_passable_via(edge, normalized)
    Availability.new(to: edge.to, via: via) unless via.empty?
  end
end

#can_fire?(event_name, metadata: {}) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
# File 'lib/statecraft/introspection.rb', line 13

def can_fire?(event_name, metadata: {})
  graph = statecraft_graph
  branches = graph.events[event_name.to_sym]
  return false unless branches

  edge = branches[statecraft_current_state]
  return false unless edge

  statecraft_guards_pass?(edge, event_name.to_sym, .normalize())
end

#transitioned_to?(state_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def transitioned_to?(state_name)
  history.where(to_state: state_name.to_s).exists?
end