Class: BridgeCache::Plugins::CSVDump

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

Constant Summary collapse

MAX_ROW_INTERVAL =
5000

Class Method Summary collapse

Class Method Details

.bulk_import(iterator, model) ⇒ Object



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

def self.bulk_import(iterator, model)
  ids = []
  rows = []
  klass = "BridgeCache::#{model.camelcase}".constantize

  csv_column_names = klass.csv_mapping.keys
  database_column_names = klass.csv_mapping.values

  iterator.each_row(model.pluralize) do |row|
    row = remove_bad_columns(klass, BridgeCache::Plugins::DataTransform.set_bridge_id(row).to_h)
    row = klass.format_import_row(row)
    rows << csv_column_names.map { |column| row[column] }
    ids << row['bridge_id']

    if rows.length >= BridgeCache.batch_size
      perform_bulk_import(klass, database_column_names, rows)
      rows = []
    end
  end

  perform_bulk_import(klass, database_column_names, rows)
  ids
end

.dump_row(clazz, row) ⇒ Object



49
50
51
52
# File 'app/lib/bridge_cache/plugins/csv_dump.rb', line 49

def self.dump_row(clazz, row)
  instance = initialze_row(clazz, row)
  dump_rows([instance])
end

.dump_to_table(clazz, file_path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/lib/bridge_cache/plugins/csv_dump.rb', line 31

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|
    rows << initialze_row(clazz, row) if count < MAX_ROW_INTERVAL
    if count % MAX_ROW_INTERVAL == 0 || count == total
      dump_rows(rows)
      count = 0
      rows = []
    end
    count += 1
  end
end