Sha256: bf1cdd5f9a4a7ad41b5c256eb710dac88d214d59b78932030aa2a86083c1576c

Contents?: true

Size: 1.39 KB

Versions: 26

Compression:

Stored size: 1.39 KB

Contents

require 'arql/vd'

module Arql::Commands
  module VD
    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_vd(table_name)
        Arql::VD.new do |vd|
          table_info(table_name).each { |row| vd << row }
        end
      end

      def table_info(table_name)
        t = []
        t << ['PK', 'Name', 'SQL Type', 'Ruby Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
        connection = ::ActiveRecord::Base.connection
        connection.columns(table_name).each do |column|
          pk = if [connection.primary_key(table_name)].flatten.include?(column.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 'vd' do |name|
      table_name = VD::get_table_name(name)
      VD::table_info_vd(table_name)
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
arql-0.3.31 lib/arql/commands/vd.rb
arql-0.3.30 lib/arql/commands/vd.rb
arql-0.3.29 lib/arql/commands/vd.rb
arql-0.3.28 lib/arql/commands/vd.rb
arql-0.3.27 lib/arql/commands/vd.rb
arql-0.3.26 lib/arql/commands/vd.rb
arql-0.3.25 lib/arql/commands/vd.rb
arql-0.3.24 lib/arql/commands/vd.rb
arql-0.3.23 lib/arql/commands/vd.rb
arql-0.3.22 lib/arql/commands/vd.rb
arql-0.3.21 lib/arql/commands/vd.rb
arql-0.3.20 lib/arql/commands/vd.rb
arql-0.3.19 lib/arql/commands/vd.rb
arql-0.3.18 lib/arql/commands/vd.rb
arql-0.3.17 lib/arql/commands/vd.rb
arql-0.3.16 lib/arql/commands/vd.rb
arql-0.3.15 lib/arql/commands/vd.rb
arql-0.3.14 lib/arql/commands/vd.rb
arql-0.3.13 lib/arql/commands/vd.rb
arql-0.3.12 lib/arql/commands/vd.rb