Class: RailsAiBridge::Tools::Schema::TableFormatter
- Inherits:
-
Object
- Object
- RailsAiBridge::Tools::Schema::TableFormatter
- 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
-
#call ⇒ String
Markdown representation of the table.
- #initialize(name:, data:, source: nil) ⇒ void constructor
Constructor Details
#initialize(name:, data:, source: nil) ⇒ void
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
#call ⇒ String
Returns 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 |