Module: Takenoko::Exporter

Extended by:
Exporter
Included in:
Exporter
Defined in:
lib/takenoko/exporter.rb

Instance Method Summary collapse

Instance Method Details

#convert_to_csv(table) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/takenoko/exporter.rb', line 33

def convert_to_csv(table)
  CSV.generate do |csv|
    csv << table[:header]
    table[:rows].each do |row|
      csv << table[:header].map {|col| row[col]}
    end
  end
end

#table_to_db(table) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/takenoko/exporter.rb', line 6

def table_to_db(table)
  tb_class = Object.const_get("::"+table[:class_name])
  raise "Class not found:#{table[:class_name]}" unless tb_class <= ActiveRecord::Base
  tb_class.destroy_all if table[:truncate_all_data]
  table[:rows].each do |r|
    if db_r = tb_class.find_by(table[:find_column] => r[table[:find_column]])
      db_r.update(r) if table[:allow_overwrite]
    else
      tb_class.new(r).save!
    end
  end
end

#table_to_file(table) ⇒ Object



19
20
21
# File 'lib/takenoko/exporter.rb', line 19

def table_to_file(table)
  public_send("table_to_#{table[:file_extension]}",table)
end