Sha256: a4b37309fd075ca062fc0b47e30f19ae4258feb6f24a0f0beacb16ce7be38a87

Contents?: true

Size: 1.6 KB

Versions: 28

Compression:

Stored size: 1.6 KB

Contents

require 'terminal-table'

module Arql::Commands
  module Table
    class << self
      def get_table_name(name)
        name = name.to_s
        return name if name =~ /^[a-z]/
        if Object.const_defined?(name)
          klass = Object.const_get(name)
          return klass.table_name if klass < ActiveRecord::Base
        end
        name
      end

      def table_info_table(table_name)
        Terminal::Table.new do |t|
          table_info(table_name).each { |row| t << (row || :separator) }
        end
      end

      def table_info(table_name)
        t = []
        t << ['PK', 'Name', 'SQL Type', 'Ruby Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
        t << nil
        connection = ::ActiveRecord::Base.connection
        connection.columns(table_name).each do |column|
          pk = if column.name == connection.primary_key(table_name)
                 'Y'
               else
                 ''
               end
          t << [pk, column.name, column.sql_type,
                column.sql_type_metadata.type, column.sql_type_metadata.limit || '',
                column.sql_type_metadata.precision || '', column.sql_type_metadata.scale || '', column.default || '',
                column.null, column.comment || '']
        end
        t
      end
    end

    Pry.commands.block_command 't' do |name|
      table_name = Table::get_table_name(name)
      puts
      puts "Table: #{table_name}"
      puts Table::table_info_table(table_name)
    end
  end
end

module Kernel
  def table(name)
    Arql::Commands::Table::table_info(Arql::Commands::Table::get_table_name(name))
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
arql-0.2.7 lib/arql/commands/table.rb
arql-0.2.6 lib/arql/commands/table.rb
arql-0.2.5 lib/arql/commands/table.rb
arql-0.2.4 lib/arql/commands/table.rb
arql-0.2.3 lib/arql/commands/table.rb
arql-0.2.2 lib/arql/commands/table.rb
arql-0.2.1 lib/arql/commands/table.rb
arql-0.2.0 lib/arql/commands/table.rb
arql-0.1.33 lib/arql/commands/table.rb
arql-0.1.32 lib/arql/commands/table.rb
arql-0.1.31 lib/arql/commands/table.rb
arql-0.1.30 lib/arql/commands/table.rb
arql-0.1.29 lib/arql/commands/table.rb
arql-0.1.28 lib/arql/commands/table.rb
arql-0.1.27 lib/arql/commands/table.rb
arql-0.1.26 lib/arql/commands/table.rb
arql-0.1.25 lib/arql/commands/table.rb
arql-0.1.24 lib/arql/commands/table.rb
arql-0.1.23 lib/arql/commands/table.rb
arql-0.1.22 lib/arql/commands/table.rb