Class: PaperTrailDiff::LiveEndpointProvider

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

Overview

Selects safe reload or validated caller-owned loading for current endpoints.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree:, traversal:, reload:) ⇒ LiveEndpointProvider

: (tree: AssociationTree, traversal: AssociationTraversal, reload: bool) -> void

Parameters:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/paper_trail_diff/live_endpoint_provider.rb', line 11

def initialize(tree:, traversal:, reload:)
  unless [true, false].include?(reload)
    raise ConfigurationError, 'reload_live_endpoints: must be true or false'
  end

  @reload = reload
  @comparison_payload = Instrumentation.comparison_payload(
    association_paths: tree.paths,
    reload_live_endpoints: reload
  ).freeze
  @loader = if reload
              LiveEndpointBatchLoader.new(tree: tree)
            else
              PreloadedEndpointBatchLoader.new(tree: tree, traversal: traversal)
            end
  @loaded = {} #: Hash[identity, untyped]
end

Instance Attribute Details

#comparison_payloadHash[Symbol, untyped] (readonly)

Signature:

  • Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


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

def comparison_payload
  @comparison_payload
end

#reloadBoolean (readonly)

Signature:

  • bool

Returns:

  • (Boolean)


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

def reload
  @reload
end

Instance Method Details

#call(records) ⇒ Hash[identity, untyped]

Loads each endpoint at most once for the life of this provider, which is one public call. A batch that resolves every root up front and then asks again for one of them costs nothing the second time, and every root in the result reflects the same instant rather than whenever its turn came. : (Array) -> Hash[identity, untyped]

Parameters:

  • (Array[untyped])

Returns:

  • (Hash[identity, untyped])


34
35
36
37
38
39
40
41
42
43
# File 'lib/paper_trail_diff/live_endpoint_provider.rb', line 34

def call(records)
  missing = records.reject { |record| @loaded.key?(Endpoint.identity(record)) }
  return known(records) if missing.empty?

  loaded = Instrumentation.instrument('load_live_endpoints', payload(missing)) do
    @loader.call(missing)
  end
  @loaded.merge!(loaded)
  known(records)
end

#known(records) ⇒ Hash[identity, untyped]

: (Array) -> Hash[identity, untyped]

Parameters:

  • (Array[untyped])

Returns:

  • (Hash[identity, untyped])


51
52
53
54
55
56
# File 'lib/paper_trail_diff/live_endpoint_provider.rb', line 51

def known(records)
  records.to_h do |record|
    identity = Endpoint.identity(record)
    [identity, @loaded.fetch(identity)]
  end
end

#payload(records) ⇒ Hash[Symbol, untyped]

: (Array) -> Hash[Symbol, untyped]

Parameters:

  • (Array[untyped])

Returns:

  • (Hash[Symbol, untyped])


59
60
61
62
63
64
65
# File 'lib/paper_trail_diff/live_endpoint_provider.rb', line 59

def payload(records)
  {
    endpoint_count: records.length,
    model_types: records.map { |record| record.class.base_class.name.to_s }.uniq.sort.freeze,
    reload_live_endpoints: reload
  }
end