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.
-
#destroy ⇒ Object
Delete this object, but only after running callbacks for it.
- #destroy! ⇒ Object
-
#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
update!() will increment the lock_version if the table has the column, but will not check it.
Instance Attribute Details
#new_record ⇒ Object Also known as: new_record?
Returns the value of attribute new_record.
13 14 15 |
# File 'lib/dynamoid/persistence.rb', line 13 def new_record @new_record end |
Instance Method Details
#delete ⇒ Object
Delete this object from the datastore.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/dynamoid/persistence.rb', line 297 def delete = range_key ? {:range_key => dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {} # Add an optimistic locking check if the lock_version column exists if(self.class.attributes[:lock_version]) conditions = {:if => {}} conditions[:if][:lock_version] = if changes[:lock_version].nil? self.lock_version else changes[:lock_version][0] end [:conditions] = conditions end Dynamoid.adapter.delete(self.class.table_name, self.hash_key, ) rescue Dynamoid::Errors::ConditionalCheckFailedException raise Dynamoid::Errors::StaleObjectError.new(self, 'delete') end |
#destroy ⇒ Object
Delete this object, but only after running callbacks for it.
283 284 285 286 287 288 |
# File 'lib/dynamoid/persistence.rb', line 283 def destroy ret = run_callbacks(:destroy) do self.delete end (ret == false) ? false : self end |
#destroy! ⇒ Object
290 291 292 |
# File 'lib/dynamoid/persistence.rb', line 290 def destroy! destroy || raise(Dynamoid::Errors::RecordNotDestroyed.new(self)) end |
#dump ⇒ Object
Dump this object’s attributes into hash form, fit to be persisted into the datastore.
319 320 321 322 323 324 325 |
# File 'lib/dynamoid/persistence.rb', line 319 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.
227 228 229 |
# File 'lib/dynamoid/persistence.rb', line 227 def persisted? !new_record? end |
#save(options = {}) ⇒ Object
Run the callbacks and then persist this object in the datastore.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/dynamoid/persistence.rb', line 234 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.
217 218 219 220 221 222 |
# File 'lib/dynamoid/persistence.rb', line 217 def touch(name = nil) now = DateTime.now self.updated_at = now attributes[name] = now if name save end |
#update(conditions = {}, &block) ⇒ Object
273 274 275 276 277 278 |
# File 'lib/dynamoid/persistence.rb', line 273 def update(conditions = {}, &block) update!(conditions, &block) true rescue Dynamoid::Errors::StaleObjectError false end |
#update!(conditions = {}, &block) ⇒ Object
update!() will increment the lock_version if the table has the column, but will not check it. Thus, a concurrent save will never cause an update! to fail, but an update! may cause a concurrent save to fail.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/dynamoid/persistence.rb', line 254 def update!(conditions = {}, &block) run_callbacks(:update) do = range_key ? {:range_key => dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {} begin new_attrs = Dynamoid.adapter.update_item(self.class.table_name, self.hash_key, .merge(:conditions => conditions)) do |t| if(self.class.attributes[:lock_version]) t.add(lock_version: 1) end yield t end load(new_attrs) rescue Dynamoid::Errors::ConditionalCheckFailedException raise Dynamoid::Errors::StaleObjectError.new(self, 'update') end end end |