module Dinamo::Model::Persistence

Public Instance Methods

create_or_update(*args) click to toggle source
# File lib/dinamo/model/persistence.rb, line 109
def create_or_update(*args)
  new_record? ? create_record(*args) : update_record(*args)
end
create_record(validate: true) click to toggle source
# File lib/dinamo/model/persistence.rb, line 113
def create_record(validate: true)
  if !validate || valid?
    self.class.adapter.insert(**self.class.symbolize(attributes))
    self.new_record = false
    self
  else
    fail Exceptions::ValidationError
  end
end
destroy() click to toggle source
# File lib/dinamo/model/persistence.rb, line 77
def destroy
  returned_value =
    if persisted?
      self.class.adapter.delete(primary_keys)
      true
    else
      false
    end
  (@destroyed = true) && freeze
  returned_value
end
destroyed?() click to toggle source
# File lib/dinamo/model/persistence.rb, line 89
def destroyed?
  !!@destroyed
end
new_record=(bool) click to toggle source
# File lib/dinamo/model/persistence.rb, line 69
def new_record=(bool)
  @new_record = bool
end
new_record?() click to toggle source
# File lib/dinamo/model/persistence.rb, line 73
def new_record?
  @new_record
end
persisted?() click to toggle source
# File lib/dinamo/model/persistence.rb, line 93
def persisted?
  !(new_record? || destroyed?)
end
reload!() click to toggle source
# File lib/dinamo/model/persistence.rb, line 164
def reload!
  fresh_object = self.class.get(primary_keys)
  @attributes = fresh_object.instance_variable_get(:'@attributes')
  @new_record = false
  self
end
save(*args) click to toggle source
# File lib/dinamo/model/persistence.rb, line 97
def save(*args)
  !!save!(*args)
rescue Exceptions::ValidationError
  false
end
save!(*args) click to toggle source
# File lib/dinamo/model/persistence.rb, line 103
def save!(*args)
  with_callback :save, *args do
    !!create_or_update(*args)
  end
end
transaction(&block) click to toggle source
# File lib/dinamo/model/persistence.rb, line 123
def transaction(&block)
  returned_value = block.call
  unless returned_value
    silent_assign(previous_attributes)
    clear_previous_attributes
  end
  returned_value
end
update(validate: false, **new_attributes) click to toggle source
# File lib/dinamo/model/persistence.rb, line 132
def update(validate: false, **new_attributes)
  transaction do
    with_callback :update do
      self.attributes = new_attributes
      return save if changed?
      true
    end
  end
rescue Exceptions::PrimaryKeyError
  false
end
update!(validate: false, **new_attributes) click to toggle source
# File lib/dinamo/model/persistence.rb, line 144
def update!(validate: false, **new_attributes)
  transaction do
    with_callback :update do
      self.attributes = new_attributes
      return save! if changed?
      true
    end
  end
end
update_record(validate: true) click to toggle source
# File lib/dinamo/model/persistence.rb, line 154
def update_record(validate: true)
  if !validate || valid?
    symbolized = self.class.symbolize(variable_attributes)
    updated_object = self.class.adapter.update(primary_keys, **symbolized)
    self.attributes = updated_object.attributes
  else
    fail Exceptions::ValidationError
  end
end