class FixedWidthGenerator::Parser

Public Class Methods

new(definition, file) click to toggle source
# File lib/aba_generator/fixed_width/parser.rb, line 3
def initialize(definition, file)
  @definition = definition
  @file       = file
end

Public Instance Methods

parse() click to toggle source
# File lib/aba_generator/fixed_width/parser.rb, line 8
def parse
  @parsed = {}
  @content = read_file
  unless @content.empty?
    @definition.sections.each do |section|
      rows = fill_content(section)
      raise FixedWidthGenerator::RequiredSectionNotFoundError.new("Required section '#{section.name}' was not found.") unless rows > 0 || section.optional
    end
  end
  @parsed
end

Private Instance Methods

add_to_section(section, line) click to toggle source
# File lib/aba_generator/fixed_width/parser.rb, line 38
def add_to_section(section, line)
  if section.singular
    @parsed[section.name] = section.parse(line)
  else
    @parsed[section.name] ||= []
    @parsed[section.name] << section.parse(line)
  end
end
fill_content(section) click to toggle source
# File lib/aba_generator/fixed_width/parser.rb, line 26
def fill_content(section)
  matches = 0
  loop do
    line = @content.first
    break unless section.match(line)
    add_to_section(section, line)
    matches += 1
    @content.shift
  end
  matches
end
read_file() click to toggle source
# File lib/aba_generator/fixed_width/parser.rb, line 22
def read_file
  @file.readlines.map(&:chomp)
end