Class: EdgeStateMachine::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/edge-state-machine/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Event.



5
6
7
8
9
10
# File 'lib/edge-state-machine/event.rb', line 5

def initialize(name, machine, &transitions)
  @machine = machine
  @name = name
  @transitions = []
  instance_eval(&transitions) if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/edge-state-machine/event.rb', line 3

def name
  @name
end

#successObject (readonly)

Returns the value of attribute success.



3
4
5
# File 'lib/edge-state-machine/event.rb', line 3

def success
  @success
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



3
4
5
# File 'lib/edge-state-machine/event.rb', line 3

def timestamp
  @timestamp
end

Instance Method Details

#fire(obj, options = {}) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/edge-state-machine/event.rb', line 12

def fire(obj, options = {})
  current_state = obj.current_state(@machine.name)
  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.possible?(obj)

  next_state = @machine.states[transition.find_next_state(obj)]
  raise NoStateFound.new("Invalid state #{transition.to.to_s} for transition.") if next_state.nil?
  transition.execute(obj)

  current_state.execute_action(:exit, obj)
  #klass._previous_state = current_state.name.to_s
  next_state.execute_action(:enter, obj)

  obj.set_current_state(next_state, @machine.name, options)
  true
end