class DataImporter::CsvReader
Attributes
csv[RW]
headers[RW]
mappings[RW]
seprator[RW]
Public Class Methods
new(file_name, mappings=nil)
click to toggle source
# File lib/data_importer/csv_reader.rb, line 5 def initialize(file_name, mappings=nil) @seprator = "," file = File.open(file_name, 'r') @csv = CSV.new(file, headers: true) @headers = File.open(file_name, 'r') {|f| f.readline.chomp}.split(@seprator) @mappings = mappings || get_default_mappings end
Public Instance Methods
get_mapped_row_hash()
click to toggle source
# File lib/data_importer/csv_reader.rb, line 17 def get_mapped_row_hash csv_row = row return nil unless csv_row csv_row = csv_row.to_hash mappings.each { |mk, mv| csv_row[mv] = csv_row.delete(mk)} csv_row end
row()
click to toggle source
# File lib/data_importer/csv_reader.rb, line 13 def row @csv.shift end
Private Instance Methods
get_default_mappings()
click to toggle source
# File lib/data_importer/csv_reader.rb, line 26 def get_default_mappings @headers.inject({}) {|mh, h| mh[h] = get_field_name(h); mh} end
get_field_name(field)
click to toggle source
# File lib/data_importer/csv_reader.rb, line 30 def get_field_name(field) field.downcase.gsub(/\s+/, ' ').gsub(" ","_").to_sym end