Class: CSVReader::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, mandatory_field) ⇒ Reader

Returns a new instance of Reader.



19
20
21
22
23
# File 'lib/csv_reader.rb', line 19

def initialize(file, mandatory_field)
  @file = file
  check_header(mandatory_field)
  @mandatory_field = mandatory_field
end

Instance Method Details

#foreachObject

Iterates through the CSV file



26
27
28
29
30
31
32
# File 'lib/csv_reader.rb', line 26

def foreach
  row_num = 0
  Ccsv.foreach(@file) do |r|
    yield parse_row(r) unless row_num == 0 #skips the header
    row_num += 1
  end
end

#headerObject

Retrieves the column names from CSV header



35
36
37
38
39
40
41
42
43
# File 'lib/csv_reader.rb', line 35

def header
  if @header == nil
    Ccsv.foreach(@file) do |line|
      @header = line
      break
    end
  end
  return @header
end