Class: PaperTrailDiff::AssociationSnapshot

Inherits:
Object
  • Object
show all
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 =

Returns:

  • (Object)
%i[has_many has_and_belongs_to_many].freeze
SUPPORTED_KINDS =

Returns:

  • (Object)
%i[belongs_to has_one has_many has_and_belongs_to_many].freeze
SERIALS =

Returns:

  • (Object)
SerialSequence.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, records:, identity_index_cache: nil, transition: nil) ⇒ AssociationSnapshot

: (kind: Symbol, records: Array, ?identity_index_cache: untyped, ?transition: CollectionTransition?) -> void

Parameters:



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

#kindSymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


91
92
93
# File 'lib/paper_trail_diff/snapshot.rb', line 91

def kind
  @kind
end

#recordsArray[RecordSnapshot] (readonly)

Signature:

  • Array[RecordSnapshot]

Returns:



92
93
94
# File 'lib/paper_trail_diff/snapshot.rb', line 92

def records
  @records
end

#serialInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

Parameters:

  • (Symbol)

Returns:

  • (Boolean)


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_indexCollectionIdentityIndex

: () -> 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?

Parameters:

  • (Object)
  • (Object)

Returns:

  • (Integer, nil)


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?

Parameters:

  • (Object)

Returns:

  • (Integer, nil)


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_hHash[Symbol, untyped]

: () -> Hash[Symbol, untyped]

Returns:

  • (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?

Parameters:

Returns:



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

Parameters:

Returns:



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