Class: PaperTrailDiff::SnapshotPool

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

Overview

Reuses immutable nodes whose normalized state did not change at a selected path.

Instance Method Summary collapse

Constructor Details

#initializeSnapshotPool

Returns a new instance of SnapshotPool.



7
8
9
10
# File 'lib/paper_trail_diff/snapshot_normalizer.rb', line 7

def initialize
  @records = {} #: Hash[Array[untyped], RecordSnapshot]
  @associations = {} #: Hash[String, AssociationSnapshot]
end

Instance Method Details

#association(path, snapshot) ⇒ AssociationSnapshot

: (String, AssociationSnapshot) -> AssociationSnapshot

Parameters:

Returns:



22
23
24
25
26
27
28
29
# File 'lib/paper_trail_diff/snapshot_normalizer.rb', line 22

def association(path, snapshot)
  previous = @associations[path]
  if previous && previous.kind == snapshot.kind && previous.records == snapshot.records
    return previous
  end

  @associations[path] = snapshot
end

#equivalent_record?(left, right) ⇒ Boolean

: (RecordSnapshot, RecordSnapshot) -> bool

Parameters:

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/paper_trail_diff/snapshot_normalizer.rb', line 37

def equivalent_record?(left, right)
  left.attributes == right.attributes &&
    left.associations.keys == right.associations.keys &&
    left.associations.all? do |name, association|
      other = right.associations[name]
      other && association.kind == other.kind && association.records == other.records
    end
end

#record(path, snapshot) ⇒ RecordSnapshot

: (String, RecordSnapshot) -> RecordSnapshot

Parameters:

Returns:



13
14
15
16
17
18
19
# File 'lib/paper_trail_diff/snapshot_normalizer.rb', line 13

def record(path, snapshot)
  key = [path, snapshot.type, snapshot.id]
  previous = @records[key]
  return previous if previous && equivalent_record?(previous, snapshot)

  @records[key] = snapshot
end