Sha256: 9a350a70c6a9a1ef9164351c4da8e60e8b9d662e12340b17469a9c7fb063a85e
Contents?: true
Size: 990 Bytes
Versions: 3
Compression:
Stored size: 990 Bytes
Contents
module Stateflow class Event attr_accessor :name, :transitions def initialize(name, &transitions) @name = name @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 end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
stateflow-0.1.2 | lib/stateflow/event.rb |
stateflow-0.1.1 | lib/stateflow/event.rb |
stateflow-0.1.0 | lib/stateflow/event.rb |