Class: CsvDb::Load

Inherits:
SerializationHelper::Load show all
Defined in:
lib/csv_db.rb

Class Method Summary collapse

Methods inherited from SerializationHelper::Load

load, load_records, load_table, reset_pk_sequence!, truncate_table

Class Method Details

.load_documents(io, truncate = true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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