Class: Msewage::Importer::Importers::CSV

Inherits:
Base
  • Object
show all
Defined in:
lib/msewage-importer/importers/csv.rb

Instance Method Summary collapse

Methods inherited from Base

factory, importers, #initialize, load_importers, type

Constructor Details

This class inherits a constructor from Msewage::Importer::Importers::Base

Instance Method Details

#build_hash(headers, sources) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/msewage-importer/importers/csv.rb', line 18

def build_hash(headers, sources)
  [].tap do |array_with_sources|
    sources.each do |line|
      source = {}
      line.each_with_index do |data, i|
        source[headers[i]] = data
      end
      array_with_sources.push(source)
    end
  end
end

#importObject



8
9
10
11
12
# File 'lib/msewage-importer/importers/csv.rb', line 8

def import
  sources = read_file
  headers = sources.shift
  build_hash(headers, sources)
end

#read_fileObject



14
15
16
# File 'lib/msewage-importer/importers/csv.rb', line 14

def read_file
  ::CSV.parse(File.read(file_name))
end