Class: Stateflow::Event
- Inherits:
-
Object
- Object
- Stateflow::Event
- Defined in:
- lib/stateflow/event.rb
Instance Attribute Summary collapse
-
#machine ⇒ Object
Returns the value of attribute machine.
-
#name ⇒ Object
Returns the value of attribute name.
-
#transitions ⇒ Object
writeonly
Sets the attribute transitions.
Instance Method Summary collapse
- #fire(current_state, klass, options) ⇒ Object
-
#initialize(name, machine = nil, &transitions) ⇒ Event
constructor
A new instance of Event.
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
#machine ⇒ Object
Returns the value of attribute machine.
3 4 5 |
# File 'lib/stateflow/event.rb', line 3 def machine @machine end |
#name ⇒ Object
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
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
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, ) 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, ) true end |