Sha256: 198a340a326dfbab0d812d0d2dfbf8d00dce2f8c9b0f66341730b4a3e5860f82

Contents?: true

Size: 908 Bytes

Versions: 1

Compression:

Stored size: 908 Bytes

Contents

require 'date'

class LogSlice
  class DateRange

    # @param file [File, String]
    # @param start_date [DateTime, String]
    # @param end_date [DateTime, String, nil]
    def initialize file, start_date, end_date=nil
      @file = file
      @start_date = start_date.is_a?(DateTime) ? start_date : DateTime.parse(start_date)
      @end_date = (end_date.is_a?(DateTime) || end_date.nil?) ? end_date : DateTime.parse(end_date)
    end

    def each
      file = LogSlice.new(@file).find do |line|
        date_compare @start_date, line
      end
      begin
        line = file.readline
        if @end_date.nil? || date_compare(@end_date, line) == 1
          yield line
        else
          break
        end
      end until file.eof?
    end

    private

    def date_compare date, line
      date_string = line.match(/\[([^\]]+)\]/)[1]
      date <=> DateTime.parse(date_string)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
log_slice-0.4 lib/log_slice/date_range.rb