Class: Textus::Read::GetOrRefresh

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/read/get_or_refresh.rb

Overview

Composes pure ‘Read::Get` with the refresh orchestrator: runs Get to obtain the envelope and freshness verdict, then if the verdict is stale and the rule’s ‘on_stale` policy demands action, hands off to the orchestrator. Use for interactive reads where the caller wants the freshest obtainable envelope.

Pure reads (build, projection, schema tooling) should use ‘Read::Get` directly; it has no orchestrator dependency.

Instance Method Summary collapse

Constructor Details

#initialize(container:, call:, get: nil, orchestrator: nil) ⇒ GetOrRefresh

Returns a new instance of GetOrRefresh.



12
13
14
15
16
17
18
# File 'lib/textus/read/get_or_refresh.rb', line 12

def initialize(container:, call:, get: nil, orchestrator: nil)
  @container    = container
  @call         = call
  @manifest     = container.manifest
  @get          = get || Read::Get.new(container: container, call: call)
  @orchestrator = orchestrator || build_orchestrator
end

Instance Method Details

#call(key) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/textus/read/get_or_refresh.rb', line 38

def call(key)
  envelope = @get.call(key)
  return nil if envelope.nil?
  return envelope unless envelope.freshness&.stale

  policy_set = @manifest.rules.for(key)
  refresh_policy = policy_set.refresh
  return envelope if refresh_policy.nil?

  policy = refresh_policy.to_freshness_policy
  verdict = Textus::Domain::Freshness::Verdict.stale(envelope.freshness.reason)
  action = policy.decide(verdict)
  outcome = @orchestrator.execute(action, key: key)

  case outcome
  when Textus::Domain::Outcome::Skipped
    envelope
  when Textus::Domain::Outcome::Refreshed
    outcome.envelope.with(
      freshness: Textus::Domain::Freshness.build(stale: false, reason: nil, refreshing: false),
    )
  when Textus::Domain::Outcome::Detached
    envelope.with(freshness: envelope.freshness.with(refreshing: true))
  when Textus::Domain::Outcome::Failed
    envelope.with(
      freshness: envelope.freshness.with(refresh_error: outcome.error.message),
    )
  end
end