Class: Squib::Import::CsvImporter

Inherits:
Object
  • Object
show all
Includes:
QuantityExploder
Defined in:
lib/squib/import/csv_importer.rb

Instance Method Summary collapse

Methods included from QuantityExploder

#explode_quantities

Instance Method Details

#check_duplicate_csv_headers(table) ⇒ Object



37
38
39
40
41
42
# File 'lib/squib/import/csv_importer.rb', line 37

def check_duplicate_csv_headers(table)
  if table.headers.size != table.headers.uniq.size
    dups = table.headers.select{|e| table.headers.count(e) > 1 }
    Squib.logger.warn "CSV duplicated the following column keys: #{dups.join(',')}"
  end
end

#import_to_dataframe(import, csv_opts, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/squib/import/csv_importer.rb', line 7

def import_to_dataframe(import, csv_opts, &block)
  data = import.data.nil? ? File.read(import.file) : import.data
  table = CSV.parse(data, **csv_opts.to_hash)
  check_duplicate_csv_headers(table)
  hash = Squib::DataFrame.new
  table.headers.each do |header|
    new_header = header.to_s
    new_header = new_header.strip if import.strip?
    hash[new_header] ||= table[header]
  end
  if import.strip?
    new_hash = Squib::DataFrame.new
    hash.each do |header, col|
      new_hash[header] = col.map do |str|
        str = str.strip if str.respond_to?(:strip)
        str
      end
    end
    hash = new_hash
  end
  unless block.nil?
    hash.each do |header, col|
      col.map! do |val|
        yield(header, val)
      end
    end
  end
  return explode_quantities(hash, import.explode)
end