Class: Textus::Domain::Policy::Predicates::FreshWithin

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

Overview

Parameterized predicate: the entry must have been written within ‘duration` of now. Duration strings (“1h”, “30m”, “7d”) parse via Domain::Duration.seconds. Passes when no envelope exists yet.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration:, now: nil) ⇒ FreshWithin

Returns a new instance of FreshWithin.



15
16
17
18
# File 'lib/textus/domain/policy/predicates/fresh_within.rb', line 15

def initialize(duration:, now: nil)
  @seconds = Textus::Domain::Duration.seconds(duration)
  @now = now
end

Instance Attribute Details

#reasonObject (readonly)

Returns the value of attribute reason.



13
14
15
# File 'lib/textus/domain/policy/predicates/fresh_within.rb', line 13

def reason
  @reason
end

Instance Method Details

#call(eval) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/textus/domain/policy/predicates/fresh_within.rb', line 22

def call(eval)
  return true if eval.envelope.nil? || @seconds.nil?

  written = written_at(eval.envelope)
  return true if written.nil?

  now = @now || Textus::Ports::Clock.now
  return true if now - written <= @seconds

  @reason = "entry older than #{@seconds}s (written #{written.iso8601})"
  false
end

#nameObject



20
# File 'lib/textus/domain/policy/predicates/fresh_within.rb', line 20

def name = "fresh_within"