class Fias::Import::Tables

Constants

DEFAULT_PREFIX
UUID

Attributes

files[R]

Public Class Methods

new(db, files, prefix = DEFAULT_PREFIX) click to toggle source
# File lib/fias/import/tables.rb, line 4
def initialize(db, files, prefix = DEFAULT_PREFIX)
  @db = db
  @files = files
  @prefix = prefix
end

Public Instance Methods

copy() click to toggle source
# File lib/fias/import/tables.rb, line 19
def copy
  @files.map do |name, dbf|
    Copy.new(@db, table_name(name), dbf, uuid_column_types(name))
  end
end
create() click to toggle source
# File lib/fias/import/tables.rb, line 12
def create
  @files.each do |name, dbf|
    next if dbf.blank?
    create_table(name, dbf)
  end
end

Private Instance Methods

column_for(name, column) click to toggle source
# File lib/fias/import/tables.rb, line 45
def column_for(name, column)
  alter = UUID[name]
  column_name = column.name.downcase

  parse_c_def(column.schema_definition).tap do |c_def|
    c_def[1] = :uuid if alter && alter.include?(column_name)
    c_def[1] = :text if c_def[1] == :string
  end
end
columns_for(name, dbf) click to toggle source
# File lib/fias/import/tables.rb, line 39
def columns_for(name, dbf)
  dbf.columns.map do |column|
    column_for(name, column)
  end
end
create_table(name, dbf) click to toggle source
# File lib/fias/import/tables.rb, line 31
def create_table(name, dbf)
  columns = columns_for(name, dbf)
  @db.create_table(table_name(name)) do
    primary_key :id
    columns.each { |args| column(*args) }
  end
end
parse_c_def(c_def) click to toggle source
# File lib/fias/import/tables.rb, line 55
def parse_c_def(c_def)
  c_def = c_def.strip.split(',').map(&:strip)
  name = c_def[0][1..-2]
  type = c_def[1][1..-1]
  [name, type].map(&:to_sym)
end
table_name(name) click to toggle source
# File lib/fias/import/tables.rb, line 27
def table_name(name)
  [@prefix, name].delete_if(&:blank?).join('_').to_sym
end
uuid_column_types(name) click to toggle source
# File lib/fias/import/tables.rb, line 62
def uuid_column_types(name)
  uuid = UUID[name] || []
  Hash[*uuid.zip([:uuid] * uuid.size).flatten]
end