Module: DBA::CSV

Defined in:
lib/dba/csv.rb

Class Method Summary collapse

Class Method Details

.dump(database, table_name, path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dba/csv.rb', line 5

def self.dump(database, table_name, path)
  columns = nil

  ::CSV.open(path, 'wb') do |csv|
    database[table_name].each_with_index do |row, index|
      if index.zero?
        columns = row.keys

        csv << columns
      end

      csv << row.values_at(*columns)
    end
  end
end

.load(path, database, table_name) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/dba/csv.rb', line 21

def self.load(path, database, table_name)
  dataset = database[table_name]

  rows = ::CSV.read(path)

  headers = rows.shift

  dataset.import(headers, rows)
end