class LogfileInterval::LogfileSet

Constants

ORDER_VALID_VALUES

Attributes

parser[R]

Public Class Methods

new(filenames, parser, order = :desc, &file_time_finder_block) click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 7
def initialize(filenames, parser, order = :desc, &file_time_finder_block)
  @parser    = parser
  @filenames = filenames
  @order    = order
  @file_time_finder_block = file_time_finder_block if block_given?

  reject_empty_files!
  reject_files_with_no_valid_line!

  raise ArgumentError, "invalid order value: #{@order}" unless ORDER_VALID_VALUES.include?(@order.to_sym)
end

Public Instance Methods

each(&block)
Alias for: each_parsed_line
each_line(&block) click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 36
def each_line(&block)
  return enum_for(__method__) unless block_given?
  each_by_method(__method__, &block)
end
each_parsed_line(&block) click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 30
def each_parsed_line(&block)
  return enum_for(__method__) unless block_given?
  each_by_method(__method__, &block)
end
Also aliased as: each
first()
Alias for: first_parsed_line
first_parsed_line() click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 41
def first_parsed_line
  each_parsed_line.first
end
Also aliased as: first
ordered_filenames() click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 19
def ordered_filenames
  time_for_files = time_for_files(existing_filenames)
  order_filenames_asc = time_for_files.to_a.sort_by { |arr| arr[1] }.map { |arr| arr[0] }
  case @order
  when :desc
    order_filenames_asc.reverse
  when :asc
    order_filenames_asc
  end
end

Private Instance Methods

each_by_method(method) { |line| ... } click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 52
def each_by_method(method, &block)
  ordered_filenames.each do |filename|
    tfile = Logfile.new(filename, parser, @order)
    tfile.send(method) do |line|
      yield line
    end
  end
end
existing_filenames() click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 48
def existing_filenames
  @existing_filenames ||= @filenames.select { |f| File.exist?(f) }
end
reject_empty_files!() click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 74
def reject_empty_files!
  @filenames.reject do |fname|
    !File.size?(fname)
  end
end
reject_files_with_no_valid_line!() click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 80
def reject_files_with_no_valid_line!
  @filenames.reject! do |fname|
    file = Logfile.new(fname, parser)
    !file.first_parsed_line
  end
end
time_for_files(filenames) click to toggle source
# File lib/logfile_interval/logfile_set.rb, line 61
def time_for_files(filenames)
  filenames.inject({}) do |h, filename|
    if @file_time_finder_block
      t = @file_time_finder_block.call(filename)
    else
      file = Logfile.new(filename, parser)
      t = file.first_timestamp
    end
    h[filename] = t
    h
  end
end