Sha256: 81d4597447625f50e9a97ab78965a69a5cd437499ede1283c06541ad545ef115

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module Multiflow
  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(machine, 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 nil unless transition.can_transition?(klass)

      new_state = 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)
      new_state.execute_action(:enter, klass)

      new_state
    end

    private
    def transitions(args = {})
      transition = Multiflow::Transition.new(args)
      @transitions << transition
    end

    def any
      @machine.states.keys
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
multiflow-1.0.0 lib/multiflow/event.rb