Class: ClaudeMemory::Dashboard::FactPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_memory/dashboard/fact_presenter.rb

Overview

Shapes a facts-table row into the hashes the dashboard API emits. Callers opt in to the shape they need:

  • #summary — full fact with confidence, scope, created_at, created_ago

  • #preview — predicate + truncated object for list rows

  • #with_provenance — summary + provenance chain (quote, session_id, occurred_at)

  • #list_summary — batches entity lookups across many rows to avoid N+1

All methods resolve the subject/object entities from the store passed at construction time; callers pass in raw facts-table rows (hashes) directly.

Constant Summary collapse

OBJECT_PREVIEW_CHARS =
120

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ FactPresenter

Returns a new instance of FactPresenter.



18
19
20
# File 'lib/claude_memory/dashboard/fact_presenter.rb', line 18

def initialize(store)
  @store = store
end

Instance Method Details

#list_summary(rows) ⇒ Array<Hash>

Returns summaries with batched entity resolution.

Parameters:

  • rows (Array<Hash>)

    facts-table rows

Returns:

  • (Array<Hash>)

    summaries with batched entity resolution



59
60
61
62
63
# File 'lib/claude_memory/dashboard/fact_presenter.rb', line 59

def list_summary(rows)
  ids = rows.flat_map { |r| [r[:subject_entity_id], r[:object_entity_id]] }.compact.uniq
  entities = ids.empty? ? {} : @store.entities.where(id: ids).as_hash(:id)
  rows.map { |r| serialize(r, entities) }
end

#preview(row) ⇒ Hash?

Returns object text truncated to OBJECT_PREVIEW_CHARS.

Parameters:

  • row (Hash, nil)

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/claude_memory/dashboard/fact_presenter.rb', line 31

def preview(row)
  return nil unless row
  entities = load_entities([row[:subject_entity_id], row[:object_entity_id]])
  subject = entities[row[:subject_entity_id]]
  object_entity = entities[row[:object_entity_id]]
  object_text = row[:object_literal] || object_entity&.dig(:canonical_name) || "unknown"
  truncated = object_text.to_s.length > OBJECT_PREVIEW_CHARS

  {
    id: row[:id],
    docid: row[:docid],
    subject: subject&.dig(:canonical_name) || "unknown",
    predicate: row[:predicate],
    object: truncated ? "#{object_text[0, OBJECT_PREVIEW_CHARS]}" : object_text,
    scope: row[:scope],
    status: row[:status]
  }
end

#summary(row) ⇒ Hash?

Returns nil when row is nil.

Parameters:

  • row (Hash, nil)

    a facts-table row

Returns:

  • (Hash, nil)

    nil when row is nil



24
25
26
27
# File 'lib/claude_memory/dashboard/fact_presenter.rb', line 24

def summary(row)
  return nil unless row
  serialize(row, load_entities([row[:subject_entity_id], row[:object_entity_id]]))
end

#with_provenance(row) ⇒ Hash?

Returns summary plus :provenance array with session/date context.

Parameters:

  • row (Hash, nil)

Returns:

  • (Hash, nil)

    summary plus :provenance array with session/date context



52
53
54
55
# File 'lib/claude_memory/dashboard/fact_presenter.rb', line 52

def with_provenance(row)
  return nil unless row
  summary(row).merge(provenance: load_provenance(row[:id]))
end