Class: FSM::State

Inherits:
Object
  • Object
show all
Includes:
Options::InstanceMethods
Defined in:
lib/fsm/state.rb

Overview

A State has a name and a list of outgoing transitions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options::InstanceMethods

#assert_options

Constructor Details

#initialize(name, options = {}) ⇒ State

name: a symbol which identifies this state options   * :enter : a symbol or string or Proc   * :exit : a symbol or string or Proc

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/fsm/state.rb', line 13

def initialize(name, options = {})
  raise ArgumentError.new('Name is required') unless name
  assert_options(options, [:enter, :exit])
  @name = name
  @enter = Executable.new options[:enter] if options.has_key?(:enter)
  @exit = Executable.new options[:exit] if options.has_key?(:exit)
  @transitions = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/fsm/state.rb', line 7

def name
  @name
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



7
8
9
# File 'lib/fsm/state.rb', line 7

def transitions
  @transitions
end

Instance Method Details

#add_transition(transition) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/fsm/state.rb', line 33

def add_transition(transition)
  raise ArgumentError.new("#{self} already has a transition to '#{transition.name}'") if @transitions.has_key?(transition.name)
  raise ArgumentError.new("the transition '#{transition.name}' is already defined") if @transitions.detect() {|to_name, tr| transition.name == tr.name}
  @transitions[transition.to.name] = transition
end

#enter(target) ⇒ Object

Called when this state is entered



23
24
25
26
# File 'lib/fsm/state.rb', line 23

def enter(target)
  @enter.execute(target) if @enter
  nil
end

#exit(target) ⇒ Object

Called when this state is exited



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

def exit(target)
  @exit.execute(target) if @exit
  nil
end

#to_sObject



44
45
46
# File 'lib/fsm/state.rb', line 44

def to_s
  "State '#{self.name}'"
end

#to_statesObject

All states that are reachable form this state by one hop



40
41
42
# File 'lib/fsm/state.rb', line 40

def to_states
  @transitions.map { |to_name, transition| transition.to}
end