Class: Stateflow::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/stateflow/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, machine = nil, &transitions) ⇒ Event

Returns a new instance of Event.



5
6
7
8
9
10
11
# File 'lib/stateflow/event.rb', line 5

def initialize(name, machine=nil, &transitions)
  @name = name
  @machine = machine
  @transitions = Array.new
  
  instance_eval(&transitions)
end

Instance Attribute Details

#machineObject

Returns the value of attribute machine.



3
4
5
# File 'lib/stateflow/event.rb', line 3

def machine
  @machine
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/stateflow/event.rb', line 3

def name
  @name
end

#transitions=(value) ⇒ Object

Sets the attribute transitions

Parameters:

  • value

    the value to set the attribute transitions to.



3
4
5
# File 'lib/stateflow/event.rb', line 3

def transitions=(value)
  @transitions = value
end

Instance Method Details

#fire(current_state, klass, options) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stateflow/event.rb', line 13

def fire(current_state, klass, options)
  transition = @transitions.select{ |t| t.from.include? current_state.name }.first
  raise NoTransitionFound.new("No transition found for event #{@name}") if transition.nil?
  
  return false unless transition.can_transition?(klass)
  
  new_state = klass.machine.states[transition.find_to_state(klass)]
  raise NoStateFound.new("Invalid state #{transition.to.to_s} for transition.") if new_state.nil?
  
  current_state.execute_action(:exit, klass)
  klass._previous_state = current_state.name.to_s
  new_state.execute_action(:enter, klass)

  klass.set_current_state(new_state, options)
  true
end