Sha256: 04cf0e16fd15f6bc90a44984ea178351e58e207aedc9775b50f75d535df54f8f

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require "edge-state-machine/event"
require "edge-state-machine/machine"
require "edge-state-machine/state"
require "edge-state-machine/state_transition"
require "edge-state-machine/version"

module EdgeStateMachine
  class InvalidTransition     < StandardError; end
  class InvalidMethodOverride < StandardError; end

  module ClassMethods
    def inherited(klass)
      super
      klass.state_machines = state_machines
    end

    def state_machines
      @state_machines ||= {}
    end

    def state_machines=(value)
      @state_machines = value ? value.dup : nil
    end

    def state_machine(name = nil, options = {}, &block)
      if name.is_a?(Hash)
        options = name
        name    = nil
      end
      name ||= :default
      state_machines[name] ||= Machine.new(self, name)
      block ? state_machines[name].update(options, &block) : state_machines[name]
    end

    def define_state_query_method(state_name)
      name = "#{state_name}?"
      undef_method(name) if method_defined?(name)
      class_eval "def #{name}; current_state.to_s == %(#{state_name}) end"
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end

  def current_state(name = nil, new_state = nil, persist = false)
    sm   = self.class.state_machine(name)
    ivar = sm.current_state_variable
    if name && new_state
      if persist && respond_to?(:write_state)
        write_state(sm, new_state)
      end

      if respond_to?(:write_state_without_persistence)
        write_state_without_persistence(sm, new_state)
      end

      instance_variable_set(ivar, new_state)
    else
      instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar)
      value = instance_variable_get(ivar)
      return value if value

      if respond_to?(:read_state)
        value = instance_variable_set(ivar, read_state(sm))
      end

      value || sm.initial_state
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
edge-state-machine-0.0.1 lib/edge-state-machine.rb