class CsvDb::Load

Public Class Methods

load_documents(io, truncate = true) click to toggle source
# File lib/csv_db.rb, line 18
def self.load_documents(io, truncate = true)
  tables = {}
  curr_table = nil
  io.each do |line|
    if /BEGIN_CSV_TABLE_DECLARATION(.+)END_CSV_TABLE_DECLARATION/ =~ line
      curr_table = $1
      tables[curr_table] = {}
    else
      if tables[curr_table]["columns"]
        tables[curr_table]["records"] << FasterCSV.parse(line)[0]
      else
        tables[curr_table]["columns"] = FasterCSV.parse(line)[0]
        tables[curr_table]["records"] = []
      end
    end
  end

  tables.each_pair do |table_name, contents|
    load_table(table_name, contents, truncate)
  end
end