Module: Dynamoid::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/dynamoid/persistence.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

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.



13
14
15
# File 'lib/dynamoid/persistence.rb', line 13

def new_record
  @new_record
end

Instance Method Details

#deleteObject

Delete this object from the datastore.

Since:

  • 0.2.0



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/dynamoid/persistence.rb', line 429

def delete
  options = 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
    options[:conditions] = conditions
  end
  Dynamoid.adapter.delete(self.class.table_name, self.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



415
416
417
418
419
420
# File 'lib/dynamoid/persistence.rb', line 415

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

#destroy!Object



422
423
424
# File 'lib/dynamoid/persistence.rb', line 422

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

#dumpObject

Dump this object’s attributes into hash form, fit to be persisted into the datastore.

Since:

  • 0.2.0



451
452
453
454
455
456
457
# File 'lib/dynamoid/persistence.rb', line 451

def dump
  Hash.new.tap do |hash|
    self.class.attributes.each do |attribute, options|
      hash[attribute] = dump_field(self.read_attribute(attribute), options)
    end
  end
end

#persisted?Boolean

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

Returns:

  • (Boolean)

Since:

  • 0.2.0



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

def persisted?
  !new_record?
end

#save(options = {}) ⇒ Object

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

Since:

  • 0.2.0



368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/dynamoid/persistence.rb', line 368

def save(options = {})
  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
end

#touch(name = nil) ⇒ Object

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



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

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

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



405
406
407
408
409
410
# File 'lib/dynamoid/persistence.rb', line 405

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.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/dynamoid/persistence.rb', line 386

def update!(conditions = {}, &block)
  run_callbacks(:update) do
    options = 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, options.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