module AASM::Persistence::SequelPersistence::InstanceMethods

def aasm_ensure_initial_state


foo.aasm_state # => nil
foo.valid?
foo.aasm_state = nil
foo.aasm_state # => 1
foo = Foo.find(:first)


foo.aasm_state # => "open" (where :open is the initial state)
foo.valid?
foo.aasm_state # => nil
foo = Foo.new

that the initial state gets populated before validation on create
Ensures that if the aasm_state column is nil and the record is new
def aasm_ensure_initial_state
  AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name|
    aasm(state_machine_name).enter_initial_state if
      (new? || values.key?(self.class.aasm(state_machine_name).attribute_name)) &&
        send(self.class.aasm(state_machine_name).attribute_name).to_s.strip.empty?
  end
end

def aasm_new_record?

def aasm_new_record?
  new?
end

def aasm_raise_invalid_record

def aasm_raise_invalid_record
  raise Sequel::ValidationFailed.new(self)
end

def aasm_read_attribute(name)

def aasm_read_attribute(name)
  send(name)
end

def aasm_save

http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/InstanceMethods.html#method-i-save
Returns nil if fails silently
def aasm_save
  !save(raise_on_failure: false).nil?
end

def aasm_transaction(requires_new, requires_lock)

def aasm_transaction(requires_new, requires_lock)
  self.class.db.transaction(savepoint: requires_new) do
    if requires_lock
      # http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/InstanceMethods.html#method-i-lock-21
      requires_lock.is_a?(String) ? lock!(requires_lock) : lock!
    end
    yield
  end
end

def aasm_update_column(attribute_name, value)

def aasm_update_column(attribute_name, value)
  this.update(attribute_name => value)
end

def aasm_write_attribute(name, value)

def aasm_write_attribute(name, value)
  send("#{name}=", value)
end

def before_create

def before_create
  super
end

def before_validation

def before_validation
  aasm_ensure_initial_state
  super
end