Module: Dynamoid::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/dynamoid/persistence.rb,
lib/dynamoid/persistence/save.rb,
lib/dynamoid/persistence/import.rb,
lib/dynamoid/persistence/upsert.rb,
lib/dynamoid/persistence/update_fields.rb

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 Classes: Import, Save, UpdateFields, Upsert

Constant Summary collapse

UNIX_EPOCH_DATE =
Date.new(1970, 1, 1).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#new_recordObject Also known as: new_record?

Returns the value of attribute new_record.



19
20
21
# File 'lib/dynamoid/persistence.rb', line 19

def new_record
  @new_record
end

Instance Method Details

#decrement(attribute, by = 1) ⇒ Object

Initializes attribute to zero if nil and subtracts the value passed as by (default is 1). Only makes sense for number-based attributes. Returns self.



372
373
374
375
376
# File 'lib/dynamoid/persistence.rb', line 372

def decrement(attribute, by = 1)
  self[attribute] ||= 0
  self[attribute] -= by
  self
end

#decrement!(attribute, by = 1) ⇒ Object

Wrapper around decrement that saves the record. Returns true if the record could be saved.



380
381
382
383
# File 'lib/dynamoid/persistence.rb', line 380

def decrement!(attribute, by = 1)
  decrement(attribute, by)
  save
end

#deleteObject

Delete this object from the datastore.

Since:

  • 0.2.0



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/dynamoid/persistence.rb', line 402

def delete
  options = range_key ? { range_key: Dumping.dump_field(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?
        lock_version
      else
        changes[:lock_version][0]
      end
    options[:conditions] = conditions
  end
  Dynamoid.adapter.delete(self.class.table_name, hash_key, options)
rescue Dynamoid::Errors::ConditionalCheckFailedException
  raise Dynamoid::Errors::StaleObjectError.new(self, 'delete')
end

#destroyObject

Delete this object, but only after running callbacks for it.

Since:

  • 0.2.0



388
389
390
391
392
393
# File 'lib/dynamoid/persistence.rb', line 388

def destroy
  ret = run_callbacks(:destroy) do
    delete
  end
  ret == false ? false : self
end

#destroy!Object



395
396
397
# File 'lib/dynamoid/persistence.rb', line 395

def destroy!
  destroy || (raise Dynamoid::Errors::RecordNotDestroyed, self)
end

#increment(attribute, by = 1) ⇒ Object

Initializes attribute to zero if nil and adds the value passed as by (default is 1). Only makes sense for number-based attributes. Returns self.



357
358
359
360
361
# File 'lib/dynamoid/persistence.rb', line 357

def increment(attribute, by = 1)
  self[attribute] ||= 0
  self[attribute] += by
  self
end

#increment!(attribute, by = 1) ⇒ Object

Wrapper around increment that saves the record. Returns true if the record could be saved.



365
366
367
368
# File 'lib/dynamoid/persistence.rb', line 365

def increment!(attribute, by = 1)
  increment(attribute, by)
  save
end

#persisted?Boolean

Is this object persisted in the datastore? Required for some ActiveModel integration stuff.

Returns:

  • (Boolean)

Since:

  • 0.2.0



266
267
268
# File 'lib/dynamoid/persistence.rb', line 266

def persisted?
  !new_record?
end

#save(_options = {}) ⇒ Object

Run the callbacks and then persist this object in the datastore.

Since:

  • 0.2.0



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/dynamoid/persistence.rb', line 273

def save(_options = {})
  self.class.create_table

  if new_record?
    run_callbacks(:create) do
      run_callbacks(:save) do
        Save.call(self)
      end
    end
  else
    run_callbacks(:save) do
      Save.call(self)
    end
  end
end

#touch(name = nil) ⇒ Object

Set updated_at and any passed in field to current DateTime. Useful for things like last_login_at, etc.



256
257
258
259
260
261
# File 'lib/dynamoid/persistence.rb', line 256

def touch(name = nil)
  now = DateTime.now
  self.updated_at = now
  attributes[name] = now if name
  save
end

#update(conditions = {}, &block) ⇒ Object



348
349
350
351
352
353
# File 'lib/dynamoid/persistence.rb', line 348

def update(conditions = {}, &block)
  update!(conditions, &block)
  true
rescue Dynamoid::Errors::StaleObjectError
  false
end

#update!(conditions = {}) ⇒ 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.



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/dynamoid/persistence.rb', line 324

def update!(conditions = {})
  run_callbacks(:update) do
    options = range_key ? { range_key: Dumping.dump_field(read_attribute(range_key), self.class.attributes[range_key]) } : {}

    begin
      new_attrs = Dynamoid.adapter.update_item(self.class.table_name, hash_key, options.merge(conditions: conditions)) do |t|
        t.add(lock_version: 1) if self.class.attributes[:lock_version]

        if Dynamoid::Config.timestamps
          time_now = DateTime.now.in_time_zone(Time.zone)
          time_now_dumped = Dumping.dump_field(time_now, self.class.attributes[:updated_at])
          t.set(updated_at: time_now_dumped)
        end

        yield t
      end
      load(Undumping.undump_attributes(new_attrs, self.class.attributes))
    rescue Dynamoid::Errors::ConditionalCheckFailedException
      raise Dynamoid::Errors::StaleObjectError.new(self, 'update')
    end
  end

end

#update_attribute(attribute, value) ⇒ Object

Update a single attribute, saving the object afterwards.

Parameters:

  • attribute (Symbol)

    the attribute to update

  • value (Object)

    the value to assign it

Since:

  • 0.2.0



314
315
316
317
# File 'lib/dynamoid/persistence.rb', line 314

def update_attribute(attribute, value)
  write_attribute(attribute, value)
  save
end

#update_attributes(attributes) ⇒ Object

Updates multiple attributes at once, saving the object once the updates are complete.

Parameters:

  • attributes (Hash)

    a hash of attributes to update

Since:

  • 0.2.0



294
295
296
297
# File 'lib/dynamoid/persistence.rb', line 294

def update_attributes(attributes)
  attributes.each { |attribute, value| write_attribute(attribute, value) }
  save
end

#update_attributes!(attributes) ⇒ Object

Updates multiple attributes at once, saving the object once the updates are complete. Raises a Dynamoid::Errors::DocumentNotValid exception if there is vaidation and it fails.

Parameters:

  • attributes (Hash)

    a hash of attributes to update



303
304
305
306
# File 'lib/dynamoid/persistence.rb', line 303

def update_attributes!(attributes)
  attributes.each { |attribute, value| write_attribute(attribute, value) }
  save!
end