Module: Dynamoid::Persistence
Overview
Persistence is responsible for dumping objects to and marshalling objects from the datastore. It tries to reserialize values to be of the same type as when they were passed in, based on the fields in the class.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#new_record ⇒ Object
(also: #new_record?)
Returns the value of attribute new_record.
Instance Method Summary collapse
-
#delete ⇒ Object
Delete this object from the datastore and all indexes.
-
#destroy ⇒ Object
Delete this object, but only after running callbacks for it.
-
#dump ⇒ Object
Dump this object’s attributes into hash form, fit to be persisted into the datastore.
-
#persisted? ⇒ Boolean
Is this object persisted in the datastore? Required for some ActiveModel integration stuff.
-
#save(options = {}) ⇒ Object
Run the callbacks and then persist this object in the datastore.
-
#touch(name = nil) ⇒ Object
Set updated_at and any passed in field to current DateTime.
- #update(conditions = {}, &block) ⇒ Object
- #update!(conditions = {}, &block) ⇒ Object
Instance Attribute Details
#new_record ⇒ Object Also known as: new_record?
Returns the value of attribute new_record.
11 12 13 |
# File 'lib/dynamoid/persistence.rb', line 11 def new_record @new_record end |
Instance Method Details
#delete ⇒ Object
Delete this object from the datastore and all indexes.
199 200 201 202 203 |
# File 'lib/dynamoid/persistence.rb', line 199 def delete delete_indexes = range_key ? {:range_key => dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {} Dynamoid::Adapter.delete(self.class.table_name, self.hash_key, ) end |
#destroy ⇒ Object
Delete this object, but only after running callbacks for it.
189 190 191 192 193 194 |
# File 'lib/dynamoid/persistence.rb', line 189 def destroy run_callbacks(:destroy) do self.delete end self end |
#dump ⇒ Object
Dump this object’s attributes into hash form, fit to be persisted into the datastore.
208 209 210 211 212 213 214 |
# File 'lib/dynamoid/persistence.rb', line 208 def dump Hash.new.tap do |hash| self.class.attributes.each do |attribute, | hash[attribute] = dump_field(self.read_attribute(attribute), ) end end end |
#persisted? ⇒ Boolean
Is this object persisted in the datastore? Required for some ActiveModel integration stuff.
151 152 153 |
# File 'lib/dynamoid/persistence.rb', line 151 def persisted? !new_record? end |
#save(options = {}) ⇒ Object
Run the callbacks and then persist this object in the datastore.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/dynamoid/persistence.rb', line 158 def save( = {}) self.class.create_table if new_record? conditions = { :unless_exists => [self.class.hash_key]} conditions[:unless_exists] << range_key if(range_key) run_callbacks(:create) { persist(conditions) } else persist end self end |
#touch(name = nil) ⇒ Object
Set updated_at and any passed in field to current DateTime. Useful for things like last_login_at, etc.
141 142 143 144 145 146 |
# File 'lib/dynamoid/persistence.rb', line 141 def touch(name = nil) now = DateTime.now self.updated_at = now attributes[name] = now if name save end |
#update(conditions = {}, &block) ⇒ Object
179 180 181 182 183 184 |
# File 'lib/dynamoid/persistence.rb', line 179 def update(conditions = {}, &block) update!(conditions, &block) true rescue Dynamoid::Errors::ConditionalCheckFailedException false end |
#update!(conditions = {}, &block) ⇒ Object
173 174 175 176 177 |
# File 'lib/dynamoid/persistence.rb', line 173 def update!(conditions = {}, &block) = range_key ? {:range_key => dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {} new_attrs = Dynamoid::Adapter.update_item(self.class.table_name, self.hash_key, .merge(:conditions => conditions), &block) load(new_attrs) end |