Class: LogSlice::DateRange

Inherits:
Object
  • Object
show all
Defined in:
lib/log_slice/date_range.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, start_date, end_date = nil) ⇒ DateRange

Returns a new instance of DateRange.

Parameters:

  • file (File, String)
  • start_date (DateTime, String)
  • end_date (DateTime, String, nil) (defaults to: nil)


9
10
11
12
13
# File 'lib/log_slice/date_range.rb', line 9

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

Instance Method Details

#eachObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/log_slice/date_range.rb', line 15

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