Sha256: d3541fa8b30436a926a1e39331c4f05138f285e69c4f81f8aea15ec9c4607931

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module Ahnnotate
  class Table
    attr_accessor :name
    attr_accessor :columns
    attr_accessor :indexes
    attr_accessor :foreign_keys

    def initialize(**args)
      args.each do |key, value|
        public_send("#{key}=", value)
      end
    end

    def string(comment:)
      tabularizer =
        Function::Tabularize.new(
          prefix: "#{comment}   ",
          cell_divider: "    "
        )

      output = StringIO.new
      output.puts "#{comment} == Schema Information"
      output.puts comment
      output.puts "#{comment} Table name: #{@name}"
      output.puts comment
      output.print tabularizer.call(columns, [:name, :type, :details])
      output.puts comment

      if indexes.any?
        output.puts "#{comment} Indexes:"
        output.puts comment
        output.print tabularizer.call(indexes, [:name, :presentable_columns, :presentable_unique, :comment])
        output.puts comment
      end

      if foreign_keys.any?
        output.puts "#{comment} Foreign keys:"
        output.puts comment
        output.puts tabularizer.call(foreign_keys, [:from, :to, :name])
        output.puts comment
      end

      output.string
    end

    private

    def longest_column_name_length
      @longest_column_name_length ||= @columns.map(&:name).map(&:size).max
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ahnnotate-0.5.1 lib/ahnnotate/table.rb
ahnnotate-0.5.0 lib/ahnnotate/table.rb