Class: Textus::Application::Reads::GetOrRefresh

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

Overview

Composes pure ‘Reads::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 ‘Reads::Get` directly; it has no orchestrator dependency.

Instance Method Summary collapse

Constructor Details

#initialize(ctx:, get:, orchestrator:) ⇒ GetOrRefresh

Returns a new instance of GetOrRefresh.



13
14
15
16
17
# File 'lib/textus/application/reads/get_or_refresh.rb', line 13

def initialize(ctx:, get:, orchestrator:)
  @ctx          = ctx
  @get          = get
  @orchestrator = orchestrator
end

Instance Method Details

#call(key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/textus/application/reads/get_or_refresh.rb', line 19

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

  policy_set = @ctx.store.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["stale_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: { "stale" => false, "stale_reason" => nil, "refreshing" => false },
    )
  when Textus::Domain::Outcome::Detached
    envelope.with(freshness: envelope.freshness.merge("refreshing" => true))
  when Textus::Domain::Outcome::Failed
    envelope.with(
      freshness: envelope.freshness.merge("refresh_error" => outcome.error.message),
    )
  end
end