Class: ETL::Parser::FixedWidthParser

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

Overview

Parser for fixed with files

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 = {}) ⇒ FixedWidthParser

Initialize the parser

  • source: The source object

  • options: Parser options Hash



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

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

Instance Method Details

#eachObject

Return each row



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

def each
  Dir.glob(file).each do |file|
    open(file).each do |line|
      row = {}
      lines_skipped = 0
      fields.each do |name, f|
        if lines_skipped < source.skip_lines
          lines_skipped += 1
          next
        end
        # TODO make strip optional?
        row[name] = line[f.field_start, f.field_length].strip
      end
      yield row
    end
  end
end

#fieldsObject

Return a map of defined fields



33
34
35
# File 'lib/etl/parser/fixed_width_parser.rb', line 33

def fields
  @fields ||= {}
end