Sha256: 4591cbcb07f21a3070ad1f80d5a9661cc5bfe1617bde6f557c0f64c5db9c797f

Contents?: true

Size: 1.52 KB

Versions: 11

Compression:

Stored size: 1.52 KB

Contents

require File.join(File.dirname(__FILE__), 'state_transition')

module AASM
  module SupportingClasses
    class Event
      attr_reader :name, :success
      
      def initialize(name, options = {}, &block)
        @name = name
        @success = options[:success]
        @transitions = []
        instance_eval(&block) if block
      end

      def fire(obj, to_state=nil, *args)
        transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
        raise AASM::InvalidTransition, "Event '#{name}' cannot transition from '#{obj.aasm_current_state}'" if transitions.size == 0

        next_state = nil
        transitions.each do |transition|
          next if to_state and !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 execute_success_callback(obj)
        case success
        when String, Symbol:
          obj.send(success)
        when Array:
          success.each { |meth| obj.send(meth) }
        when Proc:
          success.call(obj)
        end
      end

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

Version data entries

11 entries across 11 versions & 6 rubygems

Version Path
bloom-aasm-2.0.3 lib/event.rb
bloom-bloom-aasm-2.0.3 lib/event.rb
netguru-aasm-2.0.6 lib/event.rb
rubyist-aasm-2.0.4 lib/event.rb
rubyist-aasm-2.0.5 lib/event.rb
runcoderun-aasm-2.0.2.1 lib/event.rb
runcoderun-aasm-2.0.2.3 lib/event.rb
runcoderun-aasm-2.0.2.4 lib/event.rb
runcoderun-aasm-2.0.5 lib/event.rb
aasm-2.0.4 lib/event.rb
aasm-2.0.5 lib/event.rb