Module: ActiveStorage::AwsRecord::Item

Extended by:
ActiveSupport::Concern
Includes:
Persistence
Included in:
Attachment, Blob, VariantRecord
Defined in:
lib/active_storage/aws_record/item.rb

Overview

Single-table key machinery, mixed into the gem’s three entities only (Blob, Attachment, VariantRecord) — never into application owner models. It turns each entity’s logical keys into the physical DynamoDB keys for the resolved Schema mode, and stamps them before a write.

Including classes must implement #logical_keys returning { h:, r:, item_id: }:

  • h — the partition adjacency key (the ns#…#… string),

  • r — the sort adjacency key,

  • item_id— a globally-unique id string (the base-table partition in Mode B).

Instance Method Summary collapse

Methods included from Persistence

#==, #changed?, #dynamodb_client, #hash, #mark_destroyed!, #mark_persisted!, #read_attribute, #write_attribute

Instance Method Details

#ns_key(*parts) ⇒ Object



58
59
60
# File 'lib/active_storage/aws_record/item.rb', line 58

def ns_key(*parts)
  self.class.ns_key(*parts)
end

#physical_keyObject

The raw key for this instance (DB attribute names → values).



63
64
65
# File 'lib/active_storage/aws_record/item.rb', line 63

def physical_key
  self.class.physical_key(**logical_keys)
end

#schemaObject



54
55
56
# File 'lib/active_storage/aws_record/item.rb', line 54

def schema
  ActiveStorage::AwsRecord.schema
end

#stamp_physical_keys!Object

Stamp the physical key attributes from this item’s logical keys. Only on create: a key attribute cannot be updated in DynamoDB, and re-stamping would mark it dirty and break the next update_item.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_storage/aws_record/item.rb', line 70

def stamp_physical_keys!
  return unless new_record?

  keys = logical_keys
  if schema.range_mode?
    self.dynamo_partition_key = keys[:h]
    self.dynamo_range_key = keys[:r]
  else
    self.dynamo_partition_key = keys[:item_id]
    self.dynamo_range_key = 0
    self.dynamo_index_partition = keys[:h]
    self.dynamo_index_sort = keys[:r]
  end
end