Sha256: 52e91e9f4d5f80a00ab2b22c80d0505063b5d6d1395189b33a7105280d59603b
Contents?: true
Size: 1.07 KB
Versions: 2
Compression:
Stored size: 1.07 KB
Contents
module Stateflow class Event attr_accessor :name, :transitions, :machine def initialize(name, machine=nil, &transitions) @name = name @machine = machine @transitions = Array.new instance_eval(&transitions) end 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.current_state = new_state new_state.execute_action(:enter, klass) true end private def transitions(args = {}) transition = Stateflow::Transition.new(args) @transitions << transition end def any @machine.states.keys end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
stateflow-0.2.1 | lib/stateflow/event.rb |
stateflow-0.2.0 | lib/stateflow/event.rb |