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.



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

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.



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

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

#deleteObject

Delete this object from the datastore.

Since:

  • 0.2.0



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/dynamoid/persistence.rb', line 396

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



382
383
384
385
386
387
# File 'lib/dynamoid/persistence.rb', line 382

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

#destroy!Object



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

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.



351
352
353
354
355
# File 'lib/dynamoid/persistence.rb', line 351

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.



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

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



260
261
262
# File 'lib/dynamoid/persistence.rb', line 260

def persisted?
  !new_record?
end

#save(_options = {}) ⇒ Object

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

Since:

  • 0.2.0



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/dynamoid/persistence.rb', line 267

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.



250
251
252
253
254
255
# File 'lib/dynamoid/persistence.rb', line 250

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

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



342
343
344
345
346
347
# File 'lib/dynamoid/persistence.rb', line 342

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.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/dynamoid/persistence.rb', line 318

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



308
309
310
311
# File 'lib/dynamoid/persistence.rb', line 308

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



288
289
290
291
# File 'lib/dynamoid/persistence.rb', line 288

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



297
298
299
300
# File 'lib/dynamoid/persistence.rb', line 297

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