Sha256: 2e861c616caba0681fe80203e7425249207478c55c8c36e4d9c967b75388c768

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

module ActiveModel
  module StateMachine
    class Event
      attr_reader :name, :success

      def initialize(machine, name, options = {}, &block)
        @machine, @name, @transitions = machine, name, []
        if machine
          machine.klass.send(:define_method, "#{name}!") do |*args|
            machine.fire_event(name, self, true, *args)
          end

          machine.klass.send(:define_method, name.to_s) do |*args|
            machine.fire_event(name, self, false, *args)
          end
        end
        update(options, &block)
      end

      def fire(obj, to_state = nil, *args)
        transitions = @transitions.select { |t| t.from == obj.current_state(@machine ? @machine.name : nil) }
        raise InvalidTransition if transitions.size == 0

        next_state = nil
        transitions.each do |transition|
          next if to_state && !Array(transition.to).include?(to_state)
          if transition.perform(obj)
            next_state = to_state || Array(transition.to).first
            transition.execute(obj, *args)
            break
          end
        end
        next_state
      end

      def transitions_from_state?(state)
        @transitions.any? { |t| t.from? state }
      end

      def ==(event)
        if event.is_a? Symbol
          name == event
        else
          name == event.name
        end
      end

      def update(options = {}, &block)
        if options.key?(:success) then @success = options[:success] end
        if block                  then instance_eval(&block)        end
        self
      end

      private
        def transitions(trans_opts)
          Array(trans_opts[:from]).each do |s|
            @transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym}))
          end
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
cassandra_object-0.6.0.pre vendor/activemodel/lib/active_model/state_machine/event.rb
activemodel-3.0.pre lib/active_model/state_machine/event.rb
recliner-0.0.1 vendor/activemodel/lib/active_model/state_machine/event.rb