Module: Listen::FSM::ClassMethods

Defined in:
lib/listen/fsm.rb

Instance Method Summary collapse

Instance Method Details

#start_state(new_start_state = nil) ⇒ Object

Obtain or set the start state Passing a state name sets the start state


17
18
19
20
21
22
23
24
25
# File 'lib/listen/fsm.rb', line 17

def start_state(new_start_state = nil)
  if new_start_state
    new_start_state.is_a?(Symbol) or raise ArgumentError, "state name must be a Symbol (got #{new_start_state.inspect})"
    @start_state = new_start_state
  else
    defined?(@start_state) or raise ArgumentError, "`start_state :<state>` must be declared before `new`"
    @start_state
  end
end

#state(state_name, to: nil, &block) ⇒ Object

Declare an FSM state and optionally provide a callback block to fire on state entry Options:

  • to: a state or array of states this state can transition to


35
36
37
38
# File 'lib/listen/fsm.rb', line 35

def state(state_name, to: nil, &block)
  state_name.is_a?(Symbol) or raise ArgumentError, "state name must be a Symbol (got #{state_name.inspect})"
  states[state_name] = State.new(state_name, to, &block)
end

#statesObject

The valid states for this FSM, as a hash with state name symbols as keys and State objects as values.


28
29
30
# File 'lib/listen/fsm.rb', line 28

def states
  @states ||= {}
end