Class: RailsAiContext::Hydrators::HydrationFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_context/hydrators/hydration_formatter.rb

Overview

Formats SchemaHint objects into Markdown for tool output.

Class Method Summary collapse

Class Method Details

.format(hydration_result) ⇒ Object

Format a HydrationResult into a Markdown section.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rails_ai_context/hydrators/hydration_formatter.rb', line 8

def self.format(hydration_result)
  return "" unless hydration_result&.any?

  lines = [ "## Schema Hints", "" ]
  hydration_result.hints.each do |hint|
    lines << format_hint(hint)
    lines << ""
  end

  if hydration_result.warnings.any?
    hydration_result.warnings.each { |w| lines << "_#{w}_" }
    lines << ""
  end

  lines.join("\n")
end

.format_hint(hint) ⇒ Object

Format a single SchemaHint as a compact Markdown block.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rails_ai_context/hydrators/hydration_formatter.rb', line 26

def self.format_hint(hint)
  lines = []
  lines << "### #{hint.model_name} #{hint.confidence}"
  lines << "**Table:** `#{hint.table_name}` (pk: `#{hint.primary_key}`)"

  if hint.columns.any?
    col_summary = hint.columns.first(10).map { |c|
      "`#{c[:name]}` #{c[:type]}#{c[:null] == false ? ' NOT NULL' : ''}"
    }
    col_summary << "... #{hint.columns.size - 10} more" if hint.columns.size > 10
    lines << "**Columns:** #{col_summary.join(', ')}"
  end

  if hint.associations.any?
    assoc_list = hint.associations.map { |a| "`#{a[:type]}` :#{a[:name]}" }
    lines << "**Associations:** #{assoc_list.join(', ')}"
  end

  if hint.validations.any?
    val_list = hint.validations.map { |v|
      attrs = v[:attributes]&.join(", ") || ""
      "#{v[:kind]}(#{attrs})"
    }
    lines << "**Validations:** #{val_list.join(', ')}"
  end

  lines.join("\n")
end