Class: PaperTrailDiff::Diff

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

Overview

The complete structured difference between two normalized endpoints.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_presence_change: nil, attributes: {}, associations: {}) ⇒ Diff

: (?record_presence_change: ValueChange?, ?attributes: attribute_changes, ?associations: association_diffs) -> void

Parameters:

  • record_presence_change: (ValueChange, nil) (defaults to: nil)
  • attributes: (attribute_changes) (defaults to: {})
  • associations: (association_diffs) (defaults to: {})


113
114
115
116
117
118
# File 'lib/paper_trail_diff/value_objects.rb', line 113

def initialize(record_presence_change: nil, attributes: {}, associations: {})
  @record_presence_change = record_presence_change
  @attributes = Support.immutable_copy(attributes)
  @associations = Support.immutable_copy(associations)
  freeze
end

Instance Attribute Details

#associationsHash[String, ToOneAssociationDiff | CollectionAssociationDiff] (readonly)

Signature:

  • Hash[String, ToOneAssociationDiff | CollectionAssociationDiff]

Returns:



110
111
112
# File 'lib/paper_trail_diff/value_objects.rb', line 110

def associations
  @associations
end

#attributesHash[String, ValueChange] (readonly)

Signature:

  • Hash[String, ValueChange]

Returns:



109
110
111
# File 'lib/paper_trail_diff/value_objects.rb', line 109

def attributes
  @attributes
end

#record_presence_changeValueChange? (readonly)

Signature:

  • ValueChange?

Returns:



108
109
110
# File 'lib/paper_trail_diff/value_objects.rb', line 108

def record_presence_change
  @record_presence_change
end

Instance Method Details

#each_changeDiff #each_changeEnumerator[TraversalEntry, Diff]

Walks only semantic changes, excluding snapshot state included for context.

Overloads:

RBS:

  • () { (TraversalEntry) -> void } -> Diff | () -> Enumerator[TraversalEntry, Diff]

Yields:

Yield Parameters:

Yield Returns:

  • (void)


73
74
75
76
77
78
# File 'lib/paper_trail_diff/traversal.rb', line 73

def each_change(&receiver)
  return enum_for(:each_change) unless receiver

  each_entry { |entry| receiver.call(entry) if entry.change? }
  self
end

#each_entryDiff #each_entryEnumerator[TraversalEntry, Diff]

Walks semantic changes and the nested state carried by added or removed records.

Overloads:

RBS:

  • () { (TraversalEntry) -> void } -> Diff | () -> Enumerator[TraversalEntry, Diff]

Yields:

Yield Parameters:

Yield Returns:

  • (void)


63
64
65
66
67
68
# File 'lib/paper_trail_diff/traversal.rb', line 63

def each_entry(&receiver)
  return enum_for(:each_entry) unless receiver

  DiffTraversal.new(self, receiver).call
  self
end

#empty?Boolean

: () -> bool

Returns:

  • (Boolean)


121
122
123
# File 'lib/paper_trail_diff/value_objects.rb', line 121

def empty?
  record_presence_change.nil? && attributes.empty? && associations.empty?
end

#to_hHash[Symbol, untyped]

: () -> Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


126
127
128
129
130
131
132
# File 'lib/paper_trail_diff/value_objects.rb', line 126

def to_h
  {
    record_presence_change: Support.serialize(record_presence_change),
    attributes: Support.serialize(attributes),
    associations: Support.serialize(associations)
  }
end