Module: Statecraft::Machine::ClassMethods
- Defined in:
- lib/statecraft/machine.rb
Overview
Class-level DSL collected declaratively and compiled by finalize!.
Instance Method Summary collapse
- #compiled_graph ⇒ Object
- #declared_callbacks ⇒ Object
- #declared_edges ⇒ Object
- #declared_event_names ⇒ Object
- #declared_states ⇒ Object
- #event(name, from: nil, to: nil, guard: nil, lock: false, &declarations) ⇒ Object
- #events ⇒ Object
- #finalize! ⇒ Object
- #finalized? ⇒ Boolean
- #initial_state ⇒ Object
- #state(name, initial: false) ⇒ Object
- #states ⇒ Object
- #transition(from:, to:, guard: nil, lock: false) ⇒ Object
Instance Method Details
#compiled_graph ⇒ Object
109 110 111 |
# File 'lib/statecraft/machine.rb', line 109 def compiled_graph finalize! end |
#declared_callbacks ⇒ Object
133 134 135 |
# File 'lib/statecraft/machine.rb', line 133 def declared_callbacks @statecraft_declared_callbacks ||= CALLBACK_PHASES.to_h { |phase| [phase, []] } end |
#declared_edges ⇒ Object
125 126 127 |
# File 'lib/statecraft/machine.rb', line 125 def declared_edges @statecraft_declared_edges ||= [] end |
#declared_event_names ⇒ Object
129 130 131 |
# File 'lib/statecraft/machine.rb', line 129 def declared_event_names @statecraft_declared_event_names ||= [] end |
#declared_states ⇒ Object
121 122 123 |
# File 'lib/statecraft/machine.rb', line 121 def declared_states @statecraft_declared_states ||= [] end |
#event(name, from: nil, to: nil, guard: nil, lock: false, &declarations) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/statecraft/machine.rb', line 60 def event(name, from: nil, to: nil, guard: nil, lock: false, &declarations) declared_event_names << name if declarations if from || to raise CompilationError, "event #{name.inspect} takes either inline from:/to: or a block, not both" end begin @statecraft_current_event = { name: name, lock: lock } yield ensure @statecraft_current_event = nil end else raise CompilationError, "event #{name.inspect} needs from:/to: or a block" if from.nil? || to.nil? @statecraft_current_event = { name: name, lock: false } begin transition(from: from, to: to, guard: guard, lock: lock) ensure @statecraft_current_event = nil end end end |
#events ⇒ Object
101 102 103 |
# File 'lib/statecraft/machine.rb', line 101 def events compiled_graph.events.keys end |
#finalize! ⇒ Object
117 118 119 |
# File 'lib/statecraft/machine.rb', line 117 def finalize! @statecraft_compiled_graph ||= Compiler.new(self).compile end |
#finalized? ⇒ Boolean
113 114 115 |
# File 'lib/statecraft/machine.rb', line 113 def finalized? !@statecraft_compiled_graph.nil? end |
#initial_state ⇒ Object
105 106 107 |
# File 'lib/statecraft/machine.rb', line 105 def initial_state compiled_graph.initial_state end |
#state(name, initial: false) ⇒ Object
49 50 51 |
# File 'lib/statecraft/machine.rb', line 49 def state(name, initial: false) declared_states << { name: name, initial: initial } end |
#states ⇒ Object
97 98 99 |
# File 'lib/statecraft/machine.rb', line 97 def states compiled_graph.states end |
#transition(from:, to:, guard: nil, lock: false) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/statecraft/machine.rb', line 53 def transition(from:, to:, guard: nil, lock: false) declared_edges << { from: from, to: to, guards: Array(guard), lock: lock || current_event_lock, event: current_event_name } end |