Sha256: 1f3f741d0d1c0f5967cab5850892a8f2b1e068b21cf5a10b860401c5e1550854

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Ryakuzu
  class ColumnInfo
    include Ryakuzu::SchemaExtenstions

    def initialize
      @file   = File.open(Rails.root.join('db', 'schema.rb'))
      @schema = Ryakuzu::Ripper.parse(file)
    end

    def call(table, column)
      file.each do |line|
        parts = line.split ' '
        next if parts.nil? || parts.length < 1 || parts[0] == '#'
        @table_name = table_name(parts[1]) if parts[0] == 'create_table'

        next unless @table_name == table
        @field_name = field_name(parts[1]) if parts[0][0] == 't'
        next unless @field_name == column
        parts[0][0] = ''
        schema.each do |schema_line|
          schema_line[1].each do |part|
            next unless part == field_name(parts[1])
            next unless parts[1]
            hash        = remove_extra_chars(parts[0..1])
            column      = column_and_type(hash, @table_name)
            size        = parts[2..parts.count]
            parameters  = remove_extra_chars(size)
            temporary   = make_hash(parameters) if parameters
            @columns    = column.merge!(temporary)
          end
        end
      end
      Ryakuzu::Column.new(@columns.symbolize_keys).column_info
    end

    private

    def remove_extra_chars(lines)
      lines.map { |e| e.gsub(/\W/, '') }
    end

    def column_and_type(lines, table_name)
      { table: table_name, type: lines[0], column: lines[1] }
    end

    def make_hash(parameters)
      temporary = {}
      parameters.each_slice(2) do |slice|
        temporary.merge!(Hash[*slice])
      end
      temporary
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ryakuzu-0.2.6 lib/ryakuzu/column_info.rb