Class: SimplyFSM::StateMachine
- Inherits:
-
Object
- Object
- SimplyFSM::StateMachine
- Defined in:
- lib/simply_fsm/simply_fsm.rb
Overview
The DSL for defining a state machine. These methods are used within the declaration of a state_machine
.
Instance Attribute Summary collapse
-
#events ⇒ Array
readonly
All the events of the state machine.
-
#full_name ⇒ String
readonly
The name of the owning class combined with the state machine’s name.
-
#initial_state ⇒ String
readonly
The initial state of the state machine.
-
#name ⇒ String
readonly
The current value of name.
-
#states ⇒ Array
readonly
All the states of the state machine.
Instance Method Summary collapse
-
#event(event_name, transitions:, guard: nil, fail: nil) { ... } ⇒ Object
Define an event by
event_name
. -
#state(state_name, initial: false) ⇒ Object
Declare a supported
state_name
, and optionally specify one as theinitial
state.
Instance Attribute Details
#events ⇒ Array (readonly)
All the events of the state machine
39 40 41 |
# File 'lib/simply_fsm/simply_fsm.rb', line 39 def events @events end |
#full_name ⇒ String (readonly)
The name of the owning class combined with the state machine’s name
39 40 41 |
# File 'lib/simply_fsm/simply_fsm.rb', line 39 def full_name @full_name end |
#initial_state ⇒ String (readonly)
The initial state of the state machine
39 40 41 |
# File 'lib/simply_fsm/simply_fsm.rb', line 39 def initial_state @initial_state end |
#name ⇒ String (readonly)
Returns the current value of name.
39 40 41 |
# File 'lib/simply_fsm/simply_fsm.rb', line 39 def name @name end |
#states ⇒ Array (readonly)
All the states of the state machine
39 40 41 |
# File 'lib/simply_fsm/simply_fsm.rb', line 39 def states @states end |
Instance Method Details
#event(event_name, transitions:, guard: nil, fail: nil) { ... } ⇒ Object
Define an event by event_name
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/simply_fsm/simply_fsm.rb', line 83 def event(event_name, transitions:, guard: nil, fail: nil, &after) return unless event_exists?(event_name) && transitions @events << event_name may_event_name = "may_#{event_name}?" if transitions.is_a?(Array) setup_multi_transition_may_event_method transitions: transitions, guard: guard, may_event_name: may_event_name setup_multi_transition_event_method event_name, transitions: transitions, guard: guard, var_name: "@#{@name}", fail: fail || @fail_handler return end to = transitions[:to] setup_may_event_method may_event_name, transitions[:from] || :any, transitions[:when], guard setup_event_method event_name, var_name: "@#{@name}", may_event_name: may_event_name, to: to, fail: fail || @fail_handler, &after end |
#state(state_name, initial: false) ⇒ Object
Declare a supported state_name
, and optionally specify one as the initial
state.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/simply_fsm/simply_fsm.rb', line 62 def state(state_name, initial: false) return if state_name.nil? || @states.include?(state_name) status = state_name.to_sym state_machine_name = @name @states << status @initial_state = status if initial make_owner_method "#{state_name}?", lambda { send(state_machine_name) == status } end |