File: state_transition.rb

package info (click to toggle)
rails 2.3.5-1.2%2Bsqueeze8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 18,248 kB
  • ctags: 20,944
  • sloc: ruby: 122,413; makefile: 72; sql: 43; sh: 1
file content (40 lines) | stat: -rw-r--r-- 830 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module ActiveModel
  module StateMachine
    class StateTransition
      attr_reader :from, :to, :options

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

      def perform(obj)
        case @guard
        when Symbol, String
          obj.send(@guard)
        when Proc
          @guard.call(obj)
        else
          true
        end
      end
      
      def execute(obj, *args)
        case @on_transition
        when Symbol, String
          obj.send(@on_transition, *args)
        when Proc
          @on_transition.call(obj, *args)
        end
      end

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

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