module OceanDynamo::Persistence::ClassMethods


Class methods

Public Instance Methods

create(attributes = nil) { |object| ... } click to toggle source
# File lib/ocean-dynamo/persistence.rb, line 17
def create(attributes = nil, &block)
  object = new(attributes)
  yield(object) if block_given?
  object.save
  object
end
create!(attributes = nil) { |object| ... } click to toggle source
# File lib/ocean-dynamo/persistence.rb, line 25
def create!(attributes = nil, &block)
  object = new(attributes)
  yield(object) if block_given?
  object.save!
  object
end
delete(hash, range=nil) click to toggle source

Class method to delete a record. Returns true if the record existed, false if it didn't.

# File lib/ocean-dynamo/persistence.rb, line 37
def delete(hash, range=nil)
  _late_connect?
  keys = { table_hash_key.to_s => hash }
  keys[table_range_key] = range if table_range_key && range
  options = { key: keys,
              return_values: "ALL_OLD"
            }
  dynamo_table.delete_item(options).attributes ? true : false
end
delete_all() click to toggle source

Deletes all records without instantiating them first.

# File lib/ocean-dynamo/persistence.rb, line 51
def delete_all
  _late_connect?
  ean = { "#H" => table_hash_key }
  ean["#R"] = table_range_key if table_range_key
  options = {
    consistent_read: true,
    projection_expression: "#H" + (table_range_key ? ", #R" : ""),
    expression_attribute_names: ean
  }
  in_batches :scan, options do |attrs|
    if table_range_key
      delete attrs[table_hash_key.to_s], attrs[table_range_key.to_s]
    else
      delete attrs[table_hash_key.to_s]
    end
  end
  nil
end
destroy_all() click to toggle source

Destroys all records after first instantiating them.

# File lib/ocean-dynamo/persistence.rb, line 74
def destroy_all
  _late_connect?
  in_batches :scan, { consistent_read: true } do |attrs|
    new._setup_from_dynamo(attrs).destroy
  end
  nil
end