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_row(clazz, row) ⇒ Object



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

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

.dump_rows(clazz, rows) ⇒ Object



41
42
43
44
45
# File 'lib/bridge_cache/plugins/csv_dump.rb', line 41

def self.dump_rows(clazz, rows)
  rows.each do |row|
    row.save! if row.changed?
  end
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
# 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|
    if count < MAX_ROW_INTERVAL
      rows << initialze_row(clazz, row)
    end
    if count % MAX_ROW_INTERVAL == 0 || count == total
      dump_rows(clazz, rows)
      count = 0
      rows = []
    end
    count += 1
  end
end

.initialze_row(clazz, row) ⇒ Object



35
36
37
38
39
# File 'lib/bridge_cache/plugins/csv_dump.rb', line 35

def self.initialze_row(clazz, row)
  instance = clazz.find_or_create_by(bridge_id: row['id'])
  instance.assign_attributes(remove_bad_columns(clazz, BridgeCache::Plugins::DataTransform::set_bridge_id(row).to_h))
  instance
end

.remove_bad_columns(clazz, row) ⇒ Object



47
48
49
50
# File 'lib/bridge_cache/plugins/csv_dump.rb', line 47

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