# File lib/mara/model/persistence.rb, line 47 def primary_key {}.tap do |base| base[self.class.partition_key] = Mara::AttributeFormatter.format(partition_key) unless self.class.sort_key.blank? base[self.class.sort_key] = Mara::AttributeFormatter.format(sort_key) end end end
module Mara::Model::Persistence
Methods that save/update/delete a model.
@author Maddie Schipper @since 1.0.0
Public Class Methods
included(klass)
click to toggle source
@private
# File lib/mara/model/persistence.rb, line 15 def self.included(klass) klass.extend(ClassMethods) end
Public Instance Methods
destroy()
click to toggle source
Perform a destroy on the model.
@return [true, false]
# File lib/mara/model/persistence.rb, line 99 def destroy Mara.instrument('model.destroy', model: self) do Mara::Batch.delete_model(primary_key) end end
destroy!()
click to toggle source
Perform a destroy on the model.
@see destroy
@note Same as {#destroy} but will raise an error on delete failure.
@return [void]
# File lib/mara/model/persistence.rb, line 113 def destroy! Mara.instrument('model.destroy', model: self) do Mara::Batch.delete_model!(primary_key) end end
primary_key()
click to toggle source
@private
Get a primary key attribute for the item.
@return [Hash]
save()
click to toggle source
Perform validation and save the model.
@return [true, false]
# File lib/mara/model/persistence.rb, line 71 def save Mara.instrument('model.save', model: self) do next false unless valid? Mara::Batch.save_model(to_item) end end
save!()
click to toggle source
Perform validation and save the model.
@see save
@note Same as {#save} but will raise an error on validation faiure and
save failure
@return [void]
# File lib/mara/model/persistence.rb, line 88 def save! Mara.instrument('model.save', model: self) do validate! Mara::Batch.save_model!(to_item) end end
to_dynamo()
click to toggle source
@private
Converts the attributes into a DynamoDB compatable hash.
@return [Hash]
# File lib/mara/model/persistence.rb, line 33 def to_dynamo {}.tap do |formatted| attributes.each do |key, value| formatted[key] = Mara::AttributeFormatter.format(value) end end end
to_item()
click to toggle source
@private
Create a DynamoDB representation of the model.
@return [Hash]
# File lib/mara/model/persistence.rb, line 63 def to_item to_dynamo.merge(primary_key) end