class FSM::FSMState

FSMState class represents a state of the finite state machine.

@example Setup the event code for a state through block

state = FSM::FSMState.new :state_name
state.event(:event_name) do
  puts "#{@event} triggered on state #{@state}"
  :next_state
end

@example Setup multiple events specifying only the next state for each event using Hash map

state = FSM::FSMState.new :initial_state
state.event :go_left => :left_state, :go_right => :right_state

@example Trigger an event for the previous example

state.event :go_left

Attributes

events[R]

The events for this state mapped to corresponding event codes.

state[R]

The name of the state.

Public Class Methods

new(state_machine, state_name) click to toggle source

@param state_machine [FSM::FSM] the FSM object representing the state machine containing this state @param state_name [Symbol] the unique name for this state

# File lib/barebone-fsm.rb, line 52
def initialize(state_machine, state_name) 
  @fsm = state_machine
  @state = state_name
  @events = {}
end

Public Instance Methods

build(&build_block) click to toggle source

The build/#run method sets up the events described as DSL code in the build_block. Only event method is supported within the build_block with the name of the event and an optional block supplied. The operation for each such line is carried out by the {FSM::FSMState#event} method. @see FSM::FSMState#event

@param build_block [block] the block of code with sevaral event methods

@example Using block to setup events

state.build do
  event :event_name do
    puts "#{@event} triggered on state #{@state}"
    :next_state
  end
end

@example Using block to trigger events

state.build do
  event :event_name
end
# File lib/barebone-fsm.rb, line 136
def build(&build_block)
  self.instance_eval &build_block
end
Also aliased as: run
event(event_name, &event_block) click to toggle source

Setup or trigger an event. It sets up a new event when the event_block is provided or event_name is a Hash map. The event_name is triggered otherwise. If the event is nil or not already setup, then the default event is triggered.

@overload event(event_name, &event_block)

Sets up an event for this state with the given event_block parameter.
@param event_name [Symbol] the name of the event to set up
@param event_block [block] the block of code to run when this event will be triggered

@overload event(events_hash)

Sets up multiple events where each element of the Hash map corresponds to one event.
@param events_hash [Hash<Symbol,Symbol>] the Hash object mapping events to the corresponding next states

@overload event(event_name)

Triggers the event named event_name for this state.
@param event_name [Symbol] the name of the event to trigger

@example Setup an event by block

state.event(:event_name) do
  puts "#{@event} triggered on state #{@state}"
  :next_state
end

@example Setup an event by Hash

state.event :go_left => :left_state, :go_right => :right_state

@example Trigger an event

state.event :go_left
# File lib/barebone-fsm.rb, line 99
def event(event_name, &event_block)
  if block_given? then
    @events[event_name] = event_block
  elsif event_name.is_a?(Hash) then
    event_name.each{ |ev, st|
      @events[ev] = Proc.new{st}
    }
  elsif event_name and @events.has_key? event_name then
    @fsm.event = event_name
    @fsm.instance_eval &@events[event_name]
  elsif @events.has_key? :default then
    @fsm.event = :default
    @fsm.instance_eval &@events[:default]
  end
end
run(&build_block)
Alias for: build
to_s() click to toggle source

A String representation of the FSMState object.

# File lib/barebone-fsm.rb, line 61
def to_s() 
  @state.to_s + 
    ": [" + 
      @events.keys.map(&:to_s).join(', ') + 
    "]" 
end