Sha256: 1df829becb68f87dac2a94f633d728230c084aa4919f54c3fc20d0dd2fc09808

Contents?: true

Size: 1.29 KB

Versions: 16

Compression:

Stored size: 1.29 KB

Contents

module AASM
  module SupportingClasses
    class StateTransition
      attr_reader :from, :to, :opts
      alias_method :options, :opts

      def initialize(opts)
        @from, @to, @guard, @on_transition = opts[:from], opts[:to], opts[:guard], opts[:on_transition]
        @opts = opts
      end

      # TODO: should be named allowed? or similar
      def perform(obj, *args)
        case @guard
          when Symbol, String
            obj.send(@guard, *args)
          when Proc
            @guard.call(obj, *args)
          else
            true
        end
      end

      def execute(obj, *args)
        @on_transition.is_a?(Array) ?
                @on_transition.each {|ot| _execute(obj, ot, *args)} :
                _execute(obj, @on_transition, *args)
      end

      def ==(obj)
        @from == obj.from && @to == obj.to
      end

      def from?(value)
        @from == value
      end

      private

      def _execute(obj, on_transition, *args)
        case on_transition
        when Proc
          on_transition.arity == 0 ? on_transition.call : on_transition.call(obj, *args)
        when Symbol, String
          obj.send(:method, on_transition.to_sym).arity == 0 ? obj.send(on_transition) : obj.send(on_transition, *args)
        end
      end

    end
  end # SupportingClasses
end # AASM

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
aasm-3.0.16 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.15 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.14 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.13 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.12 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.11 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.10 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.9 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.8 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.7 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.6 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.5 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.4 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.3 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.2 lib/aasm/supporting_classes/state_transition.rb
aasm-3.0.1 lib/aasm/supporting_classes/state_transition.rb