Sha256: 6df5e1492c051d578f0df63e7d6c5599233293ad7fcadd3bd56f95fada1b9808

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 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, options)
      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, options)
      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

4 entries across 4 versions & 1 rubygems

Version Path
stateflow-0.5.0.beta lib/stateflow/event.rb
stateflow-0.4.2 lib/stateflow/event.rb
stateflow-0.4.1 lib/stateflow/event.rb
stateflow-0.4.0 lib/stateflow/event.rb