Class: PaperTrailDiff::AssociationSnapshot
- Inherits:
-
Object
- Object
- PaperTrailDiff::AssociationSnapshot
- Defined in:
- lib/paper_trail_diff/snapshot.rb,
sig/generated/paper_trail_diff/snapshot.rbs
Overview
A normalized association in a bounded record snapshot tree.
Constant Summary collapse
- COLLECTION_KINDS =
%i[has_many has_and_belongs_to_many].freeze
- SUPPORTED_KINDS =
%i[belongs_to has_one has_many has_and_belongs_to_many].freeze
- SERIALS =
SerialSequence.new
Instance Attribute Summary collapse
- #kind ⇒ Symbol readonly
- #records ⇒ Array[RecordSnapshot] readonly
- #serial ⇒ Integer readonly
Class Method Summary collapse
-
.collection_kind?(kind) ⇒ Boolean
: (Symbol) -> bool.
Instance Method Summary collapse
-
#identity_index ⇒ CollectionIdentityIndex
: () -> CollectionIdentityIndex.
-
#initialize(kind:, records:, identity_index_cache: nil, transition: nil) ⇒ AssociationSnapshot
constructor
: (kind: Symbol, records: Array, ?identity_index_cache: untyped, ?transition: CollectionTransition?) -> void.
-
#position(type, id) ⇒ Integer?
: (untyped, untyped) -> Integer?.
-
#position_for_id(id) ⇒ Integer?
: (untyped) -> Integer?.
-
#to_h ⇒ Hash[Symbol, untyped]
: () -> Hash[Symbol, untyped].
-
#transition_from(association) ⇒ CollectionTransition?
: (AssociationSnapshot) -> CollectionTransition?.
-
#transition_to(records, before:, after:, membership_preserved:) ⇒ AssociationSnapshot
Builds a structurally adjacent collection snapshot for internal activity carry-forward.
-
#validate_unique_identities! ⇒ void
: () -> void.
Constructor Details
#initialize(kind:, records:, identity_index_cache: nil, transition: nil) ⇒ AssociationSnapshot
: (kind: Symbol, records: Array, ?identity_index_cache: untyped, ?transition: CollectionTransition?) -> void
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/paper_trail_diff/snapshot.rb', line 96 def initialize(kind:, records:, identity_index_cache: nil, transition: nil) unless SUPPORTED_KINDS.include?(kind) raise ArgumentError, "unsupported association kind: #{kind.inspect}" end if !self.class.collection_kind?(kind) && records.length > 1 raise ArgumentError, "#{kind} association snapshots accept at most one record" end @kind = kind @records = records.frozen? ? records : records.dup.freeze @identity_index_cache = identity_index_cache || IdentityIndexCache.new @transition = transition @serial = SERIALS.next_serial freeze end |
Instance Attribute Details
#kind ⇒ Symbol (readonly)
91 92 93 |
# File 'lib/paper_trail_diff/snapshot.rb', line 91 def kind @kind end |
#records ⇒ Array[RecordSnapshot] (readonly)
92 93 94 |
# File 'lib/paper_trail_diff/snapshot.rb', line 92 def records @records end |
#serial ⇒ Integer (readonly)
93 94 95 |
# File 'lib/paper_trail_diff/snapshot.rb', line 93 def serial @serial end |
Class Method Details
.collection_kind?(kind) ⇒ Boolean
: (Symbol) -> bool
86 87 88 |
# File 'lib/paper_trail_diff/snapshot.rb', line 86 def collection_kind?(kind) COLLECTION_KINDS.include?(kind) end |
Instance Method Details
#identity_index ⇒ CollectionIdentityIndex
: () -> CollectionIdentityIndex
159 160 161 |
# File 'lib/paper_trail_diff/snapshot.rb', line 159 def identity_index @identity_index_cache.index(records) end |
#position(type, id) ⇒ Integer?
: (untyped, untyped) -> Integer?
133 134 135 |
# File 'lib/paper_trail_diff/snapshot.rb', line 133 def position(type, id) identity_index.position(type, id) end |
#position_for_id(id) ⇒ Integer?
: (untyped) -> Integer?
138 139 140 |
# File 'lib/paper_trail_diff/snapshot.rb', line 138 def position_for_id(id) identity_index.position_for_id(id) end |
#to_h ⇒ Hash[Symbol, untyped]
: () -> Hash[Symbol, untyped]
149 150 151 |
# File 'lib/paper_trail_diff/snapshot.rb', line 149 def to_h { kind: kind, records: Support.serialize(records) } end |
#transition_from(association) ⇒ CollectionTransition?
: (AssociationSnapshot) -> CollectionTransition?
128 129 130 |
# File 'lib/paper_trail_diff/snapshot.rb', line 128 def transition_from(association) @transition if @transition&.from?(association) end |
#transition_to(records, before:, after:, membership_preserved:) ⇒ AssociationSnapshot
Builds a structurally adjacent collection snapshot for internal activity carry-forward. : (Array, before: RecordSnapshot?, after: RecordSnapshot?, membership_preserved: bool) -> AssociationSnapshot
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/paper_trail_diff/snapshot.rb', line 114 def transition_to(records, before:, after:, membership_preserved:) cache = membership_preserved ? @identity_index_cache : nil transition = if before || after CollectionTransition.new(from: self, before: before, after: after) end self.class.new( kind: kind, records: records, identity_index_cache: cache, transition: transition ) end |
#validate_unique_identities! ⇒ void
This method returns an undefined value.
: () -> void
143 144 145 146 |
# File 'lib/paper_trail_diff/snapshot.rb', line 143 def validate_unique_identities! identity_index nil end |