Class: Obake::FiniteStateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/obake/finite_state_machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFiniteStateMachine

Returns a new instance of FiniteStateMachine.



5
6
7
# File 'lib/obake/finite_state_machine.rb', line 5

def initialize
  @states = {}
end

Instance Attribute Details

#current_stateObject (readonly)

Returns the value of attribute current_state.



3
4
5
# File 'lib/obake/finite_state_machine.rb', line 3

def current_state
  @current_state
end

Instance Method Details

#add_state(state) ⇒ Object



9
10
11
12
# File 'lib/obake/finite_state_machine.rb', line 9

def add_state(state)
  state.instance_variable_set(:@fsm, self)
  @states[state.id] = state
end

#change_state(transition) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/obake/finite_state_machine.rb', line 14

def change_state(transition)
  if @current_state
    @current_state.on_exit
  end

  @current_state = @states[transition.to]

  @current_state.on_enter(transition.data)
end