Module: ActiveStorage::AwsRecord::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Item
Defined in:
lib/active_storage/aws_record/persistence.rb

Overview

Generic aws-record plumbing shared by every model that participates in the Active Storage owner contract — both the gem’s own entities and the application’s owner models (which include Owner). It deliberately holds no single-table key logic (that lives in Item, mixed only into the gem’s three entities), so an application owner keeps its own key schema.

Provides: aws-record + GlobalID, the shared client, equality by class + id, changed? → aws-record dirty?, raw attribute access, and helpers to mark an instance persisted/destroyed after a raw write.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



44
45
46
# File 'lib/active_storage/aws_record/persistence.rb', line 44

def ==(other)
  other.instance_of?(self.class) && id.present? && id == other.id
end

#changed?Boolean

Active Storage’s generic owner path checks changed?; aws-record names the same concept dirty?.

Returns:

  • (Boolean)


55
56
57
# File 'lib/active_storage/aws_record/persistence.rb', line 55

def changed?
  dirty?
end

#dynamodb_clientObject



40
41
42
# File 'lib/active_storage/aws_record/persistence.rb', line 40

def dynamodb_client
  self.class.dynamodb_client
end

#hashObject



49
50
51
# File 'lib/active_storage/aws_record/persistence.rb', line 49

def hash
  [self.class, id].hash
end

#mark_destroyed!Object

Mark this instance destroyed after a raw delete.



78
79
80
# File 'lib/active_storage/aws_record/persistence.rb', line 78

def mark_destroyed!
  instance_variable_get('@data').destroyed = true
end

#mark_persisted!Object

Mark this instance persisted after a raw (non-aws-record) write.



70
71
72
73
74
75
# File 'lib/active_storage/aws_record/persistence.rb', line 70

def mark_persisted!
  data = instance_variable_get('@data')
  data.new_record = false
  data.destroyed = false
  data.clean!
end

#read_attribute(name) ⇒ Object

Raw attribute access via aws-record’s data object (it has no AR-style self[:attr]), for use inside overridden accessors.



61
62
63
# File 'lib/active_storage/aws_record/persistence.rb', line 61

def read_attribute(name)
  instance_variable_get('@data').get_attribute(name)
end

#write_attribute(name, value) ⇒ Object



65
66
67
# File 'lib/active_storage/aws_record/persistence.rb', line 65

def write_attribute(name, value)
  instance_variable_get('@data').set_attribute(name, value)
end