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:) ⇒ void constructor
Constructor Details
#initialize(name:, data:) ⇒ void
13 14 15 16 |
# File 'lib/rails_ai_bridge/tools/schema/table_formatter.rb', line 13 def initialize(name:, data:) @name = name @data = data end |
Instance Method Details
#call ⇒ String
Returns Markdown representation of the table.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rails_ai_bridge/tools/schema/table_formatter.rb', line 19 def call lines = ["## Table: #{@name}", ''] 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 |