Module: ReadOperations::InstanceMethods

Defined in:
lib/flat/read_operations.rb

Overview

Instance Methods

Defines behavior for instances of a subclass of Flat::File regarding the reading and parsing of the file contents, line by line.

Instance Method Summary collapse

Instance Method Details

#each_record(io, &block) ⇒ Object

Iterate through each record (each line of the data file). The passed block is passed a new Record representing the line.

s = SomeFile.new
s.each_record(open('/path/to/file')) do |r|
  puts r.first_name
end

– NOTE: Expects an open valid IO handle; opening and closing is out of scope ++



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flat/read_operations.rb', line 30

def each_record io, &block
  io.each_line do |line|
    line.chop!
    next if line.length.zero?

    unless (self.width - line.length).zero?
      raise Errors::RecordLengthError, "length is #{line.length} but should be #{self.width}"
    end

    yield create_record(line, io.lineno), line
  end
end