Class: Engram::UseCases::Forget

Inherits:
Object
  • Object
show all
Defined in:
lib/engram/use_cases/forget.rb

Overview

Prune stale memories from a scope. A memory is stale when its last activity (last_accessed_at, or created_at if never accessed) is older than the cutoff. ‘min_importance` keeps important memories even when they are old: only memories with importance below it are eligible. The default forgets all stale memories.

Instance Method Summary collapse

Constructor Details

#initialize(store:) ⇒ Forget

Returns a new instance of Forget.



10
11
12
# File 'lib/engram/use_cases/forget.rb', line 10

def initialize(store:)
  @store = store
end

Instance Method Details

#call(scope:, older_than:, min_importance: Float::INFINITY, now: Time.now) ⇒ Object

Returns the Array<Record> that were forgotten.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/engram/use_cases/forget.rb', line 15

def call(scope:, older_than:, min_importance: Float::INFINITY, now: Time.now)
  cutoff = now - older_than

  stale = @store.all(scope: scope).select do |record|
    timestamp = record.last_accessed_at || record.created_at
    timestamp && timestamp < cutoff && record.importance.to_f < min_importance
  end

  stale.each { |record| @store.delete(id: record.id) if record.id }
  stale
end