aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/state_machine/machine.rb
blob: 75ed8f8b65e7b7119547d6dad8f6622256c302ab (plain) (blame)
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
module ActiveModel
  module StateMachine
    class Machine
      attr_accessor :initial_state, :states, :event
      attr_reader :klass, :name

      def initialize(klass, name)
        @klass, @name, @states, @events = klass, name, [], {}
      end

      def states_for_select
        states.map { |st| [st.display_name, st.name.to_s] }
      end

      def state(name, options = {})
        @states << State.new(self, name, options)
      end

      def initial_state
        @initial_state ||= (states.first ? states.first.name : nil)
      end

      def update(options = {}, &block)
        @initial_state = options[:initial]
        instance_eval(&block)
        self
      end
    end
  end
end