Class: LogfileInterval::Logfile

Inherits:
Object
  • Object
show all
Defined in:
lib/logfile_interval/logfile.rb

Constant Summary collapse

ORDER_VALID_VALUES =
[ :asc, :desc ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, parser, order = :desc) ⇒ Logfile

Returns a new instance of Logfile.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/logfile_interval/logfile.rb', line 7

def initialize(filename, parser, order = :desc)
  @filename = filename
  @parser   = parser
  @order    = order

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

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/logfile_interval/logfile.rb', line 3

def filename
  @filename
end

#parserObject (readonly)

Returns the value of attribute parser.



3
4
5
# File 'lib/logfile_interval/logfile.rb', line 3

def parser
  @parser
end

Instance Method Details

#each_lineObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/logfile_interval/logfile.rb', line 30

def each_line
  return unless exist?
  return enum_for(:each_line) unless block_given?

  case @order
  when :asc
    each_line_ascending { |l| yield l }
  when :desc
    each_line_descending { |l| yield l }
  end
end

#each_parsed_lineObject Also known as: each



42
43
44
45
46
47
48
# File 'lib/logfile_interval/logfile.rb', line 42

def each_parsed_line
  return enum_for(:each_parsed_line) unless block_given?
  each_line do |line|
    record = parser.create_record(line)
    yield record if record
  end
end

#exist?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/logfile_interval/logfile.rb', line 15

def exist?
  filename && File.exist?(@filename)
end

#first_parsed_lineObject Also known as: first



51
52
53
# File 'lib/logfile_interval/logfile.rb', line 51

def first_parsed_line
  each_parsed_line.first
end

#first_timestampObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/logfile_interval/logfile.rb', line 19

def first_timestamp
  return unless exist?
  File.open(@filename) do |f|
    while line = f.gets
      if record = parser.create_record(line)
        return record.time
      end
    end
  end
end