Class: PrettyFSM::FSM

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty-fsm/fsm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, initial_state, while_prefix = 'while_', start_prefix = 'start_', end_prefix = 'end_') ⇒ FSM

object is an instance where all messages regarding transitions and states are sent. States are named with Symbol instances. The prefixes, are the prefixes to the message names that object will receive (if it responds to them).



9
10
11
12
13
14
15
16
17
# File 'lib/pretty-fsm/fsm.rb', line 9

def initialize(object, initial_state, while_prefix = 'while_', start_prefix = 'start_', end_prefix = 'end_')
  @object = object
  @while_prefix,@start_prefix,@end_prefix = while_prefix,start_prefix,end_prefix
  @initial_state = initial_state
  @state = nil
  @transitions = Hash.new {|h,k| h[k] = []}
  @observers = []
  if (block_given?) then self.instance_eval(&Proc.new) end
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



5
6
7
# File 'lib/pretty-fsm/fsm.rb', line 5

def object
  @object
end

#stateObject (readonly)

This will be nil if the FSM hasn’t made the initial transition yet.



4
5
6
# File 'lib/pretty-fsm/fsm.rb', line 4

def state
  @state
end

Instance Method Details

#add_observer(obj) ⇒ Object

Adds obj to the observer list. An observer receives all while, start and end (if defined) methods right after #object



48
49
50
# File 'lib/pretty-fsm/fsm.rb', line 48

def add_observer(obj)
  @observers << obj
end

#advanceObject

This method should be called periodically. It will act as a timestepper. ‘while’ methods will be called here.



21
22
23
24
25
26
27
# File 'lib/pretty-fsm/fsm.rb', line 21

def advance
  if (@state.nil?) then start(@initial_state) end
  call_fsm_method(@while_prefix)

  possible_next_state, = @transitions[@state].find {|state,condition| @object.send(condition)}
  start(possible_next_state) unless possible_next_state.nil?
end

#defined_statesObject

All “from” states defined so far with #transition



35
36
37
# File 'lib/pretty-fsm/fsm.rb', line 35

def defined_states
  @transitions.keys
end

#remove_observer(obj) ⇒ Object



52
53
54
# File 'lib/pretty-fsm/fsm.rb', line 52

def remove_observer(obj)
  @observers.delete(obj)
end

#start(new_state, *args) ⇒ Object

Transitions the FSM into the specified state, calling the ‘end’ and ‘start’ method, of the old and new states, respectively. Any additional parameter passed to this method will be received by the corresponding ‘start’ method.



41
42
43
44
45
# File 'lib/pretty-fsm/fsm.rb', line 41

def start(new_state, *args)
  call_fsm_method(@end_prefix) unless @state.nil?
  @state = new_state
  call_fsm_method(@start_prefix, args)
end

#transition(options) ⇒ Object

Adds a transition definition. Only necessary if you want the FSM to advance itself from a state



30
31
32
# File 'lib/pretty-fsm/fsm.rb', line 30

def transition(options)
  @transitions[options[:from]].push([ options[:to], options[:if] ])
end