Class: RailsAiBridge::Tools::Schema::TableFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/tools/schema/table_formatter.rb

Overview

Renders a single database table as a Markdown block with columns, indexes, and foreign keys.

Instance Method Summary collapse

Constructor Details

#initialize(name:, data:, source: nil) ⇒ void

Parameters:

  • name (String)

    table name

  • data (Hash)

    slice from schema introspection (+:columns+, :indexes, :foreign_keys, optional :partition_of / :partitioned)

  • source (Symbol, String, nil) (defaults to: nil)

    :live (verified) or :static (inferred)



15
16
17
18
19
# File 'lib/rails_ai_bridge/tools/schema/table_formatter.rb', line 15

def initialize(name:, data:, source: nil)
  @name = name
  @data = data
  @source = source
end

Instance Method Details

#callString

Returns Markdown representation of the table.

Returns:

  • (String)

    Markdown representation of the table



22
23
24
25
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
# File 'lib/rails_ai_bridge/tools/schema/table_formatter.rb', line 22

def call
  lines = ["## Table: #{ConfidenceTag.tagged(@name, @source)}", '']
  heading = partition_heading
  if heading
    lines << heading
    lines << ''
  end
  lines << '| Column | Type | Nullable | Default |'
  lines << '|--------|------|----------|---------|'

  (@data[:columns] || []).each do |col|
    lines << "| #{col[:name]} | #{col[:type]} | #{col[:null] ? 'yes' : 'no'} | #{col[:default] || '-'} |"
  end

  if @data[:indexes]&.any?
    lines << '' << '### Indexes'
    @data[:indexes].each do |idx|
      unique = idx[:unique] ? ' (unique)' : ''
      lines << "- `#{idx[:name]}` on (#{Array(idx[:columns]).join(', ')})#{unique}"
    end
  end

  if @data[:foreign_keys]&.any?
    lines << '' << '### Foreign keys'
    @data[:foreign_keys].each do |fk|
      lines << "- `#{fk[:column]}` → `#{fk[:to_table]}.#{fk[:primary_key]}`"
    end
  end

  lines.join("\n")
end