Sha256: 6d5afc89f0e793ef9dfda71495c435db10c7d58b1bfe11b480f3947a111fad44

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module AASM
  class StateMachine

    # the following two methods provide the storage of all state machines
    def self.[](klass)
      (@machines ||= {})[klass.to_s]
    end

    def self.[]=(klass, machine)
      (@machines ||= {})[klass.to_s] = machine
    end

    attr_accessor :states, :events, :initial_state, :config, :name, :global_callbacks

    def initialize(name)
      @initial_state = nil
      @states = []
      @events = {}
      @global_callbacks = {}
      @config = AASM::Configuration.new
      @name = name
    end

    # called internally by Ruby 1.9 after clone()
    def initialize_copy(orig)
      super
      @states = @states.dup
      @events = @events.dup
    end

    def add_state(state_name, klass, options)
      set_initial_state(state_name, options)

      # allow reloading, extending or redefining a state
      @states.delete(state_name) if @states.include?(state_name)

      @states << AASM::Core::State.new(state_name, klass, self, options)
    end

    def add_event(name, options, &block)
      @events[name] = AASM::Core::Event.new(name, self, options, &block)
    end

    def add_global_callbacks(name, *callbacks, &block)
      @global_callbacks[name] ||= []
      callbacks.each do |callback|
        @global_callbacks[name] << callback unless @global_callbacks[name].include? callback
      end
      @global_callbacks[name] << block if block
    end

    private

    def set_initial_state(name, options)
      @initial_state = name if options[:initial] || !initial_state
    end

  end # StateMachine
end # AASM

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aasm-4.9.0 lib/aasm/state_machine.rb
aasm-4.8.0 lib/aasm/state_machine.rb