Class: FSM
- Inherits:
-
Object
- Object
- FSM
- Defined in:
- lib/netutils/fsm.rb
Direct Known Subclasses
Defined Under Namespace
Classes: State
Constant Summary collapse
- FSM_S_INIT =
0
Instance Method Summary collapse
- #add(name, cb) ⇒ Object
- #cb ⇒ Object
- #changeto(name) ⇒ Object
-
#initialize ⇒ FSM
constructor
A new instance of FSM.
- #state_name ⇒ Object
Constructor Details
#initialize ⇒ FSM
Returns a new instance of FSM.
12 13 14 15 16 17 |
# File 'lib/netutils/fsm.rb', line 12 def initialize @state = FSM_S_INIT @fsm = Array.new @name2index = Hash.new @index2name = Array.new end |
Instance Method Details
#add(name, cb) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/netutils/fsm.rb', line 19 def add(name, cb) if @name2index.key?(name) raise(ArgumentError, "duplicated key \"#{name}\"") end @fsm.push(State.new(cb)) idx = @fsm.length - 1 @name2index[name] = idx @index2name[idx] = name end |
#cb ⇒ Object
29 30 31 |
# File 'lib/netutils/fsm.rb', line 29 def cb return @fsm[@state].cb end |
#changeto(name) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/netutils/fsm.rb', line 33 def changeto(name) if ! @name2index.key?(name) raise(ArgumentError, "unnown state: \"#{name}\"") end @state = @name2index[name] end |
#state_name ⇒ Object
40 41 42 |
# File 'lib/netutils/fsm.rb', line 40 def state_name return @index2name[@state] end |