Class: EdgeStateMachine::Event
- Inherits:
-
Object
- Object
- EdgeStateMachine::Event
- Defined in:
- lib/edge-state-machine/event.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Instance Method Summary collapse
- #fire(obj, options = {}) ⇒ Object
-
#initialize(name, machine, &transitions) ⇒ Event
constructor
A new instance of Event.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/edge-state-machine/event.rb', line 3 def name @name end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
3 4 5 |
# File 'lib/edge-state-machine/event.rb', line 3 def success @success end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
3 4 5 |
# File 'lib/edge-state-machine/event.rb', line 3 def @timestamp end |
Instance Method Details
#fire(obj, options = {}) ⇒ Object
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, = {}) 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, ) true end |