Class: ETL::Parser::DelimitedParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/etl/parser/delimited_parser.rb

Overview

Parses delimited files

Defined Under Namespace

Classes: Field

Instance Attribute Summary

Attributes inherited from Parser

#options, #source

Instance Method Summary collapse

Methods inherited from Parser

class_for_name

Constructor Details

#initialize(source, options = {}) ⇒ DelimitedParser

Initialize the parser

  • source: The Source object

  • options: Hash of options for the parser, defaults to an empty hash



8
9
10
11
# File 'lib/etl/parser/delimited_parser.rb', line 8

def initialize(source, options={})
  super
  configure
end

Instance Method Details

#eachObject

Returns each row.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/etl/parser/delimited_parser.rb', line 14

def each
  Dir.glob(file).each do |file|
    ETL::Engine.logger.debug "parsing #{file}"
    line = 0
    lines_skipped = 0
    FasterCSV.foreach(file, options) do |raw_row|
      if lines_skipped < source.skip_lines
        ETL::Engine.logger.debug "skipping line"
        lines_skipped += 1
        next
      end
      line += 1
      row = {}
      validate_row(raw_row, line, file)
      raw_row.each_with_index do |value, index|
        f = fields[index]
        row[f.name] = value
      end
      yield row
    end
  end
end

#fieldsObject

Get an array of defined fields



38
39
40
# File 'lib/etl/parser/delimited_parser.rb', line 38

def fields
  @fields ||= []
end