Class: Hifsm::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/hifsm/machine.rb

Overview

This is just a storage of current state

Instance Method Summary collapse

Constructor Details

#initialize(fsm, target, initial_state = nil) ⇒ Machine

Returns a new instance of Machine.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/hifsm/machine.rb', line 4

def initialize(fsm, target, initial_state = nil)
  @target = target || self
  @fsm = fsm

  initial_state_method_name = "initial_#{fsm.name}"
  initial_state ||= target.send(initial_state_method_name) if target.respond_to?(initial_state_method_name)
  initial_state &&= fsm.get_state!(initial_state)
  initial_state ||= fsm.initial_state!

  @state = initial_state.enter!
end

Instance Method Details

#act!(*args) ⇒ Object



16
17
18
# File 'lib/hifsm/machine.rb', line 16

def act!(*args)
  @state.act!(@target, *args)
end

#all_statesObject



28
29
30
# File 'lib/hifsm/machine.rb', line 28

def all_states
  @fsm.all_states.reject(&:sub_fsm).collect(&:to_s)
end

#fire(event, *args) ⇒ Object



32
33
34
35
36
# File 'lib/hifsm/machine.rb', line 32

def fire(event, *args)
  @state.fire(@target, event, *args) do |new_state|
    @state = new_state
  end
end

#stateObject



20
21
22
# File 'lib/hifsm/machine.rb', line 20

def state
  @state
end

#statesObject



24
25
26
# File 'lib/hifsm/machine.rb', line 24

def states
  @fsm.states
end

#to_sObject



38
39
40
# File 'lib/hifsm/machine.rb', line 38

def to_s
  @state.to_s
end