Class: BridgeCache::Plugins::CSVDump

Inherits:
Object
  • Object
show all
Defined in:
lib/bridge_cache/plugins/csv_dump.rb

Constant Summary collapse

MAX_ROW_INTERVAL =
5000

Class Method Summary collapse

Class Method Details

.dump_rows(clazz, rows) ⇒ Object



29
30
31
# File 'lib/bridge_cache/plugins/csv_dump.rb', line 29

def self.dump_rows(clazz, rows)
  rows.each {|row| row.save!}
end

.dump_to_table(clazz, file_path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bridge_cache/plugins/csv_dump.rb', line 8

def self.dump_to_table(clazz, file_path)
  count = 1
  total = 0
  rows = []
  CSV.foreach(file_path, headers: true) do |row|
    total += 1
  end
  CSV.foreach(file_path, headers: true) do |row|
    row = self.remove_bad_columns(clazz, row)
    if count < MAX_ROW_INTERVAL
      rows << clazz.new(BridgeCache::Plugins::DataTransform::set_bridge_id(row).to_h)
    end
    if count % MAX_ROW_INTERVAL == 0 || count == total
      dump_rows(clazz, rows)
      count = 0
      rows = []
    end
    count += 1
  end
end

.remove_bad_columns(clazz, row) ⇒ Object



33
34
35
36
# File 'lib/bridge_cache/plugins/csv_dump.rb', line 33

def self.remove_bad_columns(clazz, row)
  row = row.delete_if {|key, value| !clazz.column_names.include?(key)}
  row
end