Class: LogfileInterval::Logfile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, parser) ⇒ Logfile

Returns a new instance of Logfile.



5
6
7
8
# File 'lib/logfile_interval/logfile.rb', line 5

def initialize(filename, parser)
  @filename = filename
  @parser   = parser
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



25
26
27
28
29
30
31
32
33
34
# File 'lib/logfile_interval/logfile.rb', line 25

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

  f = Util::FileBackward.new(@filename)
  while(line = f.gets)
    yield line.chomp
  end
  f.close
end

#each_parsed_lineObject



36
37
38
39
40
41
42
# File 'lib/logfile_interval/logfile.rb', line 36

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)


10
11
12
# File 'lib/logfile_interval/logfile.rb', line 10

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

#first_timestampObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/logfile_interval/logfile.rb', line 14

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