Class: Obake::FiniteStateMachine
- Inherits:
-
Object
- Object
- Obake::FiniteStateMachine
- Defined in:
- lib/obake/finite_state_machine.rb
Instance Attribute Summary collapse
-
#current_state ⇒ Object
readonly
Returns the value of attribute current_state.
Instance Method Summary collapse
- #add_state(state) ⇒ Object
- #change_state(transition) ⇒ Object
-
#initialize ⇒ FiniteStateMachine
constructor
A new instance of FiniteStateMachine.
Constructor Details
#initialize ⇒ FiniteStateMachine
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_state ⇒ Object (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 |