Class: PaperTrailDiff::BranchSnapshotRefresher

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

Overview

Reconstructs selected root branches and merges them into a prior immutable snapshot.

Instance Method Summary collapse

Constructor Details

#initialize(tree:, ignore_policy:, traversal:, pool:, normalizer:, full_snapshotter:, partial_snapshotter:, association_reader:, record_transition:) ⇒ BranchSnapshotRefresher

: (tree: AssociationTree, ignore_policy: IgnorePolicy, traversal: AssociationTraversal, pool: SnapshotPool, normalizer: SnapshotNormalizer, full_snapshotter: untyped, partial_snapshotter: untyped, association_reader: untyped, record_transition: untyped) -> void

Parameters:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 8

def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
  tree:,
  ignore_policy:,
  traversal:,
  pool:,
  normalizer:,
  full_snapshotter:,
  partial_snapshotter:,
  association_reader:,
  record_transition:
)
  @tree = tree
  @ignore_policy = ignore_policy
  @traversal = traversal
  @pool = pool
  @full_snapshotter = full_snapshotter
  @partial_snapshotter = partial_snapshotter
  @components = {} #: Hash[Array[String], [AssociationTree, SnapshotNormalizer]]
  @event_refresher = ActivityEventSnapshotRefresher.new(
    traversal: traversal,
    pool: pool,
    components: method(:components),
    association_reader: association_reader,
    record_transition: record_transition
  )
  @root_refresher = ActivityRootSnapshotRefresher.new(
    tree: tree,
    traversal: traversal,
    normalizer: normalizer,
    record_after: @event_refresher.method(:record_after),
    changeset: @event_refresher.method(:deserialized_changeset),
    partial_snapshotter: method(:partial_snapshot)
  )
end

Instance Method Details

#advance_root(root_endpoint, context_endpoint, previous_snapshot, event:) ⇒ RecordSnapshot?

: (untyped, untyped, RecordSnapshot?, event: ActivityEvent) -> RecordSnapshot?

Parameters:

Returns:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 44

def advance_root(root_endpoint, context_endpoint, previous_snapshot, event:)
  handled, incremental = @root_refresher.call(
    root_endpoint,
    context_endpoint,
    previous_snapshot,
    event
  )
  return incremental if handled

  full_snapshot(root_endpoint, context_endpoint)
end

#build_components(branches) ⇒ [ AssociationTree, SnapshotNormalizer ]

: (Array) -> [AssociationTree, SnapshotNormalizer]

Parameters:

  • (Array[String])

Returns:



169
170
171
172
173
174
175
176
177
178
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 169

def build_components(branches)
  tree = @tree.select(branches)
  normalizer = SnapshotNormalizer.new(
    tree: tree,
    ignore_policy: @ignore_policy,
    traversal: @traversal,
    pool: @pool
  )
  [tree, normalizer]
end

#call(root_endpoint, context_endpoint, previous_snapshot, branches, event: nil, isolated: false) ⇒ RecordSnapshot?

: (untyped, untyped, RecordSnapshot?, Array, ?event: ActivityEvent?, ?isolated: bool) -> RecordSnapshot?

Parameters:

  • (Object)
  • (Object)
  • (RecordSnapshot, nil)
  • (Array[String])
  • event: (ActivityEvent, nil) (defaults to: nil)
  • isolated: (Boolean) (defaults to: false)

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 57

def call( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
  root_endpoint,
  context_endpoint,
  previous_snapshot,
  branches,
  event: nil,
  isolated: false
)
  return full_snapshot(root_endpoint, context_endpoint) unless previous_snapshot
  if isolated && event && branches.length > 1
    return refresh_independent_branches(
      root_endpoint,
      context_endpoint,
      previous_snapshot,
      branches,
      event
    )
  end

  endpoints = [root_endpoint, context_endpoint]
  handled, incremental = incremental_event(
    endpoints,
    previous_snapshot,
    branches,
    event,
    isolated
  )
  return incremental if handled

  partial = partial_snapshot(root_endpoint, context_endpoint, branches)
  return full_snapshot(root_endpoint, context_endpoint) unless partial

  merge(previous_snapshot, partial)
end

#components(branches) ⇒ [ AssociationTree, SnapshotNormalizer ]

: (Array) -> [AssociationTree, SnapshotNormalizer]

Parameters:

  • (Array[String])

Returns:



161
162
163
164
165
166
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 161

def components(branches)
  return @components[branches] if @components.key?(branches)

  key = branches.sort.freeze
  @components[key] ||= build_components(key)
end

#full_snapshot(root_endpoint, context_endpoint) ⇒ RecordSnapshot?

: (untyped, untyped) -> RecordSnapshot?

Parameters:

  • (Object)
  • (Object)

Returns:



181
182
183
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 181

def full_snapshot(root_endpoint, context_endpoint)
  @full_snapshotter.call(root_endpoint, context_endpoint)
end

#incremental_event(endpoints, previous, branches, event, isolated) ⇒ [ bool, RecordSnapshot? ]

: (Array, RecordSnapshot, Array, ActivityEvent?, bool) -> [bool, RecordSnapshot?]

Parameters:

Returns:



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 105

def incremental_event(endpoints, previous, branches, event, isolated)
  return [false, nil] unless isolated

  @event_refresher.call(
    endpoints.fetch(0),
    endpoints.fetch(1),
    previous,
    branches,
    event
  )
end

#merge(previous, partial) ⇒ RecordSnapshot

: (RecordSnapshot, RecordSnapshot) -> RecordSnapshot

Parameters:

Returns:



186
187
188
189
190
191
192
193
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 186

def merge(previous, partial)
  RecordSnapshot.new(
    type: previous.type,
    id: previous.id,
    attributes: previous.attributes,
    associations: previous.associations.merge(partial.associations)
  )
end

#partial_snapshot(root_endpoint, context_endpoint, branches) ⇒ RecordSnapshot?

: (untyped, untyped, Array) -> RecordSnapshot?

Parameters:

  • (Object)
  • (Object)
  • (Array[String])

Returns:



150
151
152
153
154
155
156
157
158
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 150

def partial_snapshot(root_endpoint, context_endpoint, branches)
  tree, normalizer = components(branches)
  @partial_snapshotter.call(
    root_endpoint,
    context_endpoint,
    tree: tree,
    normalizer: normalizer
  )
end

#refresh_independent_branches(root_endpoint, context_endpoint, previous_snapshot, branches, event) ⇒ RecordSnapshot?

: (untyped, untyped, RecordSnapshot, Array, ActivityEvent) -> RecordSnapshot?

Parameters:

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/paper_trail_diff/branch_snapshot_refresher.rb', line 118

def refresh_independent_branches( # rubocop:disable Metrics/MethodLength
  root_endpoint,
  context_endpoint,
  previous_snapshot,
  branches,
  event
)
  snapshot = previous_snapshot
  remaining = [] #: Array[String]
  branches.each do |branch|
    handled, incremental = @event_refresher.call(
      root_endpoint,
      context_endpoint,
      snapshot,
      [branch],
      event
    )
    if handled
      snapshot = incremental || snapshot
    else
      remaining << branch
    end
  end
  return snapshot if remaining.empty?

  partial = partial_snapshot(root_endpoint, context_endpoint, remaining)
  return full_snapshot(root_endpoint, context_endpoint) unless partial

  merge(snapshot, partial)
end