Module: Mara::Model::Persistence
- Included in:
- Base
- Defined in:
- lib/mara/model/persistence.rb
Overview
Methods that save/update/delete a model.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#destroy ⇒ true, false
Perform a destroy on the model.
-
#destroy! ⇒ void
Perform a destroy on the model.
-
#primary_key ⇒ Hash
Get a primary key attribute for the item.
-
#save ⇒ true, false
Perform validation and save the model.
-
#save! ⇒ void
Perform validation and save the model.
-
#to_dynamo ⇒ Hash
Converts the attributes into a DynamoDB compatable hash.
-
#to_item ⇒ Hash
Create a DynamoDB representation of the model.
Class Method Details
.included(klass) ⇒ Object
15 16 17 |
# File 'lib/mara/model/persistence.rb', line 15 def self.included(klass) klass.extend(ClassMethods) end |
Instance Method Details
#destroy ⇒ true, false
Perform a destroy on the model.
99 100 101 102 103 |
# 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! ⇒ void
Same as #destroy but will raise an error on delete failure.
This method returns an undefined value.
Perform a destroy on the model.
113 114 115 116 117 |
# 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 ⇒ Hash
Get a primary key attribute for the item.
47 48 49 50 51 52 53 54 55 |
# 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 |
#save ⇒ true, false
Perform validation and save the model.
71 72 73 74 75 76 77 |
# 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! ⇒ void
Same as #save but will raise an error on validation faiure and save failure
This method returns an undefined value.
Perform validation and save the model.
88 89 90 91 92 93 |
# 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 ⇒ Hash
Converts the attributes into a DynamoDB compatable hash.
33 34 35 36 37 38 39 |
# 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 ⇒ Hash
Create a DynamoDB representation of the model.
63 64 65 |
# File 'lib/mara/model/persistence.rb', line 63 def to_item to_dynamo.merge(primary_key) end |