Class: Fibrio::Parsers::CSV

Inherits:
Base
  • Object
show all
Defined in:
lib/fibrio/parsers/csv.rb

Constant Summary

Constants inherited from Base

Base::WHITESPACE

Instance Method Summary collapse

Methods inherited from Base

#close, #initialize

Constructor Details

This class inherits a constructor from Fibrio::Parsers::Base

Instance Method Details

#run {|record| ... } ⇒ Object

Yields:

  • (record)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fibrio/parsers/csv.rb', line 7

def run(&emit)
  headers = nil
  headers_enabled = @options.fetch(:headers, true)
  line_number = 0

  while (record = next_csv_record(line_number))
    line, start_line_number, line_number = record
    fields = split_csv_line(line, line_number: start_line_number)

    if headers_enabled && headers.nil?
      headers = fields
      next
    end

    validate_field_count(headers, fields, line_number) if headers
    emit.call(headers ? headers.zip(fields).to_h : fields)
  end
end