Class: PaperTrailDiff::RecordSnapshot
- Inherits:
-
Object
- Object
- PaperTrailDiff::RecordSnapshot
- 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
- #associations ⇒ Hash[String, AssociationSnapshot] readonly
- #attributes ⇒ Hash[String, untyped] readonly
- #id ⇒ Object readonly
- #identity ⇒ Array[untyped] readonly
- #type ⇒ String readonly
Instance Method Summary collapse
-
#initialize(type:, id:, attributes:, associations: {}) ⇒ RecordSnapshot
constructor
: (type: String, id: untyped, attributes: snapshot_attributes, ?associations: snapshot_associations) -> void.
-
#normalize_hash(hash) ⇒ Hash[String, untyped]
: (Hash[untyped, untyped]) -> Hash[String, untyped].
-
#reference ⇒ RecordReference
: () -> RecordReference.
-
#to_h ⇒ Hash[Symbol, untyped]
: () -> Hash[Symbol, untyped].
Constructor Details
#initialize(type:, id:, attributes:, associations: {}) ⇒ RecordSnapshot
: (type: String, id: untyped, attributes: snapshot_attributes, ?associations: snapshot_associations) -> void
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
#associations ⇒ Hash[String, AssociationSnapshot] (readonly)
170 171 172 |
# File 'lib/paper_trail_diff/snapshot.rb', line 170 def associations @associations end |
#attributes ⇒ Hash[String, untyped] (readonly)
169 170 171 |
# File 'lib/paper_trail_diff/snapshot.rb', line 169 def attributes @attributes end |
#id ⇒ Object (readonly)
167 168 169 |
# File 'lib/paper_trail_diff/snapshot.rb', line 167 def id @id end |
#identity ⇒ Array[untyped] (readonly)
168 169 170 |
# File 'lib/paper_trail_diff/snapshot.rb', line 168 def identity @identity end |
#type ⇒ String (readonly)
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]
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 |
#reference ⇒ RecordReference
: () -> RecordReference
183 184 185 |
# File 'lib/paper_trail_diff/snapshot.rb', line 183 def reference RecordReference.new(type: type, id: id) end |
#to_h ⇒ Hash[Symbol, untyped]
: () -> 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 |