Class: CSVReader::Reader
- Inherits:
-
Object
- Object
- CSVReader::Reader
- Defined in:
- lib/csv_reader.rb
Instance Method Summary collapse
-
#foreach ⇒ Object
Iterates through the CSV file.
-
#header ⇒ Object
Retrieves the column names from CSV header.
-
#initialize(file, mandatory_field) ⇒ Reader
constructor
A new instance of Reader.
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
#foreach ⇒ Object
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 |
#header ⇒ Object
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 |