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
31
# File 'lib/etl/parser/fixed_width_parser.rb', line 14

def each
  Dir.glob(file).each do |file|
    lines_skipped = 0
    open(file).each do |line|
      row = {}
      if lines_skipped < source.skip_lines
        lines_skipped += 1
        next
      end
      fields.each do |name, f|

        # 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



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

def fields
  @fields ||= {}
end