Sha256: fe2cb64c0d20cda15641f75c36da9ce261147a1042cfd7ddf87069e4c2fd4506

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

##
# = ReadOperations
#
# Defines functionality required for the successful reading, parsing and
# interpreting of a line of text contained in a flat file.
#
module ReadOperations
  module ClassMethods #:nodoc:
  end # => module ClassMethods

  ##
  # = Instance Methods
  #
  # Defines behavior for instances of a subclass of Flat::File regarding the
  # reading and parsing of the file contents, line by line.
  #
  module InstanceMethods

    # 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
    #++
    #
    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

  end # => module InstanceMethods

  def self.included receiver #:nodoc:
    receiver.extend         ClassMethods
    receiver.send :include, InstanceMethods
  end

end # => module ReadOperations

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
flat-0.1.5 lib/flat/read_operations.rb
flat-0.1.4 lib/flat/read_operations.rb
flat-0.1.3 lib/flat/read_operations.rb
flat-0.1.2 lib/flat/read_operations.rb
flat-0.1.1 lib/flat/read_operations.rb
flat-0.1.0 lib/flat/read_operations.rb