Class: FSM::State
- Inherits:
-
Object
- Object
- FSM::State
- 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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Method Summary collapse
- #add_transition(transition) ⇒ Object
-
#enter(target) ⇒ Object
Called when this state is entered.
-
#exit(target) ⇒ Object
Called when this state is exited.
-
#initialize(name, options = {}) ⇒ State
constructor
name: a symbol which identifies this state options * :enter : a symbol or string or Proc * :exit : a symbol or string or Proc.
- #to_s ⇒ Object
-
#to_states ⇒ Object
All states that are reachable form this state by one hop.
Methods included from Options::InstanceMethods
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
13 14 15 16 17 18 19 20 |
# File 'lib/fsm/state.rb', line 13 def initialize(name, = {}) raise ArgumentError.new('Name is required') unless name (, [:enter, :exit]) @name = name @enter = Executable.new [:enter] if .has_key?(:enter) @exit = Executable.new [:exit] if .has_key?(:exit) @transitions = {} end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/fsm/state.rb', line 7 def name @name end |
#transitions ⇒ Object (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
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_s ⇒ Object
44 45 46 |
# File 'lib/fsm/state.rb', line 44 def to_s "State '#{self.name}'" end |
#to_states ⇒ Object
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 |