module Dymos::Persistence

Attributes

new_record[RW]

Public Class Methods

new(params={}) click to toggle source
# File lib/dymos/persistence.rb, line 5
def initialize(params={})
  @new_record = true
  @destroyed = false
end

Public Instance Methods

delete() click to toggle source
# File lib/dymos/persistence.rb, line 50
def delete

  if persisted?
    builder = ::Dymos::Query::DeleteItem.new

    builder.name(self.table_name).key(indexes).return_values(:all_old)

    @query.each do |k, v|
      builder.send k, *v
    end if @query.present?
    @query={}

    query = builder.build
    @last_execute_query = {command: builder.command, query: query}
    ::Dymos::Client.new.command builder.command, query
  end
  @destroyed = true
  freeze
end
destroyed?() click to toggle source
# File lib/dymos/persistence.rb, line 14
def destroyed?
  @destroyed
end
new_record?() click to toggle source
# File lib/dymos/persistence.rb, line 10
def new_record?
  @new_record
end
persisted?() click to toggle source
# File lib/dymos/persistence.rb, line 18
def persisted?
  !(new_record? || destroyed?)
end
save(*) click to toggle source
# File lib/dymos/persistence.rb, line 22
def save(*)
  run_callbacks :save do
    _put
  end
rescue => e
  false
end
save!(*) click to toggle source
# File lib/dymos/persistence.rb, line 30
def save!(*)
  run_callbacks :save do
    _put || raise(::Dymos::RecordNotSaved)
  end
end
update(*) click to toggle source
# File lib/dymos/persistence.rb, line 36
def update(*)
  run_callbacks :save do
    _update
  end
rescue => e
  false
end
update!(*) click to toggle source
# File lib/dymos/persistence.rb, line 44
def update!(*)
  run_callbacks :save do
    _update || raise(::Dymos::RecordNotSaved)
  end
end

Private Instance Methods

_execute(builder) click to toggle source

def _update_record()

send :updated_at=, Time.new.iso8601 if respond_to? :updated_at
_execute

end

def _create_record()

send :created_at=, Time.new.iso8601 if respond_to? :created_at
send :updated_at=, Time.new.iso8601 if respond_to? :updated_at
_execute

end

# File lib/dymos/persistence.rb, line 116
def _execute(builder)
  query = builder.build
  @last_execute_query = {command: builder.command, query: query}
  response = ::Dymos::Client.new.command builder.command, query
  fail raise(::Dymos::RecordNotSaved) if response.nil?
  changes_applied
  @new_record = false
                    true
end
_put() click to toggle source
# File lib/dymos/persistence.rb, line 72
def _put
  send :created_at=, Time.new.iso8601 if respond_to? :created_at if @new_record
  send :updated_at=, Time.new.iso8601 if respond_to? :updated_at
  builder = ::Dymos::Query::PutItem.new
  builder.name(self.table_name).item(attributes).return_values(:all_old)

  @query.each do |k, v|
    builder.send k, *v
  end if @query.present?
  @query={}

  _execute(builder)
end
_update() click to toggle source
# File lib/dymos/persistence.rb, line 86
def _update
  send :updated_at=, Time.new.iso8601 if respond_to? :updated_at
  builder = ::Dymos::Query::UpdateItem.new

  builder.name(self.table_name).key(indexes).return_values(:all_old)

  self.changes.each do |column, change|
    builder.put(column, change[1])
  end

  @query.each do |k, v|
    builder.send k, *v
  end if @query.present?
  @query={}

  _execute(builder)
end