class TrickBag::Enumerables::FileLineReader

Reads a file containing strings, one on each line, and feeds them in its each() method. Strips all lines leading and trailing whitespace, and ignores all empty lines and lines beginning with the comment character '#'.

If calling each without a block to get an enumerator, call enumerator.close when done if not all values have been exhausted, so that the input file will be closed.

Supports specifying a starting position and maximum count. For example if the starting position is 2, then the first 2 valid lines will be discarded, and yielding will begin with the next valid line.

Similarly, specifying a maximum count will cause yielding to end after max_count objects have been yielded.

Attributes

filespec[R]
start_and_max[R]

Public Class Methods

new(filespec, start_pos = :first, max_count = :infinite) click to toggle source

@param filespec - the file from which to read lines; blank/empty lines and lines

with the first nonblank character == '#' will be ignored.

@param start_pos - the record number, zero offset, at which to begin each() processing @param max_count - the maximum number of records to be served by each()

# File lib/trick_bag/enumerables/file_line_reader.rb, line 29
def initialize(filespec, start_pos = :first, max_count = :infinite)
  @filespec = filespec
  @start_and_max = TrickBag::Numeric::StartAndMax.new(start_pos, max_count)
end

Public Instance Methods

close_file(file) click to toggle source

Closes the file if it is not null and has not already been closed

# File lib/trick_bag/enumerables/file_line_reader.rb, line 55
def close_file(file)
  if file && (! file.closed?)
    file.close
  end
end
each() { |line| ... } click to toggle source

Returns an Enumerator if called without a block; in that case, remember to close the file explicitly by calling the enumerator's close method if you finish using it before all values have been exhausted.

# File lib/trick_bag/enumerables/file_line_reader.rb, line 85
def each

  file = File.open(filespec, 'r')
  return to_enum(file) unless block_given?

  valid_record_num = 0
  yield_count = 0

  begin

    file.each do |line|
      line.strip!
      if line_valid?(line)
        if start_and_max.start_position_reached?(valid_record_num)
          yield(line)
          yield_count += 1
          if start_and_max.max_count_reached?(yield_count)
            return
          end
        end

        valid_record_num += 1
      end
    end

  ensure
    close_file(file)
  end
end
line_valid?(line) click to toggle source

@param line the line to test @return whether or not this line is valid (eligible for yielding),

specifically not empty and not a comment
# File lib/trick_bag/enumerables/file_line_reader.rb, line 49
def line_valid?(line)
  ! (line.empty? || /^#/ === line)
end
max_count() click to toggle source

@return the maximum number of objects to be yielded

# File lib/trick_bag/enumerables/file_line_reader.rb, line 41
def max_count
  start_and_max.max_count
end
start_pos() click to toggle source

@return the position of the first valid object to be yielded.

# File lib/trick_bag/enumerables/file_line_reader.rb, line 35
def start_pos
  start_and_max.start_pos
end
to_enum(file) click to toggle source

Any enumerator returned should have a close method to close the file from which lines are read. This method gets an enumerator from the superclass, and adds a 'file' attribute and a close method to it.

Calls superclass method
# File lib/trick_bag/enumerables/file_line_reader.rb, line 65
def to_enum(file)
  enumerator = super()
  enumerator.instance_variable_set(:@file, file)

  # This method is defined on the instance of the new enumerator.
  # It closes the input file.
  def enumerator.close
    if @file && (! @file.closed?)
      @file.close
      @file = nil
    end
  end

  enumerator
end
to_s() click to toggle source
# File lib/trick_bag/enumerables/file_line_reader.rb, line 116
def to_s
  "#{self.class}: filespec: #{filespec}, start_pos: #{start_pos.inspect}, max_count: #{max_count.inspect}"
end