Sha256: 4185eaa0aaa2651d5efbe516c39a8de473dbcf78686c3accbbc220c082125189

Contents?: true

Size: 1008 Bytes

Versions: 7

Compression:

Stored size: 1008 Bytes

Contents

module ActiveModel
  module StateMachine
    class State
      attr_reader :name, :options

      def initialize(name, options = {})
        @name = name
        if machine = options.delete(:machine)
          machine.klass.define_state_query_method(name)
        end
        update(options)
      end

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

      def call_action(action, record)
        action = @options[action]
        case action
        when Symbol, String
          record.send(action)
        when Proc
          action.call(record)
        end
      end

      def display_name
        @display_name ||= name.to_s.gsub(/_/, ' ').capitalize
      end

      def for_select
        [display_name, name.to_s]
      end

      def update(options = {})
        if options.key?(:display) then @display_name = options.delete(:display) end
        @options = options
        self
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 5 rubygems

Version Path
kajam-1.0.3.rc2 vendor/rails/activemodel/lib/active_model/state_machine/state.rb
radiant-1.0.0.rc2 vendor/rails/activemodel/lib/active_model/state_machine/state.rb
radiant-1.0.0.rc1 vendor/rails/activemodel/lib/active_model/state_machine/state.rb
cassandra_object-0.6.0.pre vendor/activemodel/lib/active_model/state_machine/state.rb
radiant-0.9.0.rc2 vendor/rails/activemodel/lib/active_model/state_machine/state.rb
activemodel-3.0.pre lib/active_model/state_machine/state.rb
recliner-0.0.1 vendor/activemodel/lib/active_model/state_machine/state.rb