Class: PaperTrailDiff::RecordSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/paper_trail_diff/snapshot.rb,
sig/generated/paper_trail_diff/snapshot.rbs

Overview

An immutable, ActiveRecord-independent representation of a reified record.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, id:, attributes:, associations: {}) ⇒ RecordSnapshot

: (type: String, id: untyped, attributes: snapshot_attributes, ?associations: snapshot_associations) -> void

Parameters:

  • type: (String)
  • id: (Object)
  • attributes: (snapshot_attributes)
  • associations: (snapshot_associations) (defaults to: {})


173
174
175
176
177
178
179
180
# File 'lib/paper_trail_diff/snapshot.rb', line 173

def initialize(type:, id:, attributes:, associations: {})
  @type = Support.immutable_copy(type.to_s)
  @id = Support.immutable_copy(id)
  @identity = [@type, @id].freeze
  @attributes = normalize_hash(attributes)
  @associations = normalize_hash(associations)
  freeze
end

Instance Attribute Details

#associationsHash[String, AssociationSnapshot] (readonly)

Signature:

  • Hash[String, AssociationSnapshot]

Returns:



170
171
172
# File 'lib/paper_trail_diff/snapshot.rb', line 170

def associations
  @associations
end

#attributesHash[String, untyped] (readonly)

Signature:

  • Hash[String, untyped]

Returns:

  • (Hash[String, untyped])


169
170
171
# File 'lib/paper_trail_diff/snapshot.rb', line 169

def attributes
  @attributes
end

#idObject (readonly)

Signature:

  • untyped

Returns:

  • (Object)


167
168
169
# File 'lib/paper_trail_diff/snapshot.rb', line 167

def id
  @id
end

#identityArray[untyped] (readonly)

Signature:

  • Array[untyped]

Returns:

  • (Array[untyped])


168
169
170
# File 'lib/paper_trail_diff/snapshot.rb', line 168

def identity
  @identity
end

#typeString (readonly)

Signature:

  • String

Returns:

  • (String)


166
167
168
# File 'lib/paper_trail_diff/snapshot.rb', line 166

def type
  @type
end

Instance Method Details

#normalize_hash(hash) ⇒ Hash[String, untyped]

: (Hash[untyped, untyped]) -> Hash[String, untyped]

Parameters:

  • (Hash[untyped, untyped])

Returns:

  • (Hash[String, untyped])


201
202
203
204
205
206
207
# File 'lib/paper_trail_diff/snapshot.rb', line 201

def normalize_hash(hash)
  normalized = {} #: Hash[String, untyped]
  hash.sort_by { |key, _value| key.to_s }.each do |key, value|
    normalized[Support.immutable_copy(key.to_s)] = Support.immutable_copy(value)
  end
  normalized.freeze
end

#referenceRecordReference

: () -> RecordReference

Returns:



183
184
185
# File 'lib/paper_trail_diff/snapshot.rb', line 183

def reference
  RecordReference.new(type: type, id: id)
end

#to_hHash[Symbol, untyped]

: () -> Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


188
189
190
191
192
193
194
195
196
# File 'lib/paper_trail_diff/snapshot.rb', line 188

def to_h
  value = {
    type: type,
    id: id,
    attributes: Support.serialize(attributes)
  }
  value[:associations] = Support.serialize(associations) unless associations.empty?
  value
end