Class: Textus::Domain::Policy::Refresh

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/domain/policy/refresh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl:, on_stale:, sync_budget_ms:) ⇒ Refresh

Returns a new instance of Refresh.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/textus/domain/policy/refresh.rb', line 7

def initialize(ttl:, on_stale:, sync_budget_ms:)
  on_stale_sym = on_stale.is_a?(Symbol) ? on_stale : on_stale.to_s.to_sym
  unless ALLOWED_ON_STALE.include?(on_stale_sym)
    raise Textus::UsageError.new(
      "on_stale must be one of #{ALLOWED_ON_STALE.join(", ")} (got #{on_stale.inspect})",
    )
  end

  @ttl            = ttl
  @on_stale       = on_stale_sym
  @sync_budget_ms = sync_budget_ms
end

Instance Attribute Details

#on_staleObject (readonly)

Returns the value of attribute on_stale.



5
6
7
# File 'lib/textus/domain/policy/refresh.rb', line 5

def on_stale
  @on_stale
end

#sync_budget_msObject (readonly)

Returns the value of attribute sync_budget_ms.



5
6
7
# File 'lib/textus/domain/policy/refresh.rb', line 5

def sync_budget_ms
  @sync_budget_ms
end

#ttlObject (readonly)

Returns the value of attribute ttl.



5
6
7
# File 'lib/textus/domain/policy/refresh.rb', line 5

def ttl
  @ttl
end

Instance Method Details

#to_freshness_policyObject



38
39
40
41
42
43
44
# File 'lib/textus/domain/policy/refresh.rb', line 38

def to_freshness_policy
  Textus::Domain::Freshness::Policy.new(
    ttl_seconds: ttl_seconds,
    on_stale: @on_stale,
    sync_budget_ms: @sync_budget_ms,
  )
end

#ttl_secondsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/textus/domain/policy/refresh.rb', line 20

def ttl_seconds
  return nil if @ttl.nil?

  str = @ttl.to_s.strip
  return str.to_i if str.match?(/\A\d+\z/)

  m = str.match(/\A(\d+)\s*([smhd])\z/)
  return nil unless m

  n = m[1].to_i
  case m[2]
  when "s" then n
  when "m" then n * 60
  when "h" then n * 3600
  when "d" then n * 86_400
  end
end