Class: LogfileInterval::Logfile
- Inherits:
-
Object
- Object
- LogfileInterval::Logfile
- Defined in:
- lib/logfile_interval/logfile.rb
Constant Summary collapse
- ORDER_VALID_VALUES =
[ :asc, :desc ]
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
- #each_line ⇒ Object
- #each_parsed_line ⇒ Object (also: #each)
- #exist? ⇒ Boolean
- #first_parsed_line ⇒ Object (also: #first)
- #first_timestamp ⇒ Object
-
#initialize(filename, parser, order = :desc) ⇒ Logfile
constructor
A new instance of Logfile.
Constructor Details
#initialize(filename, parser, order = :desc) ⇒ Logfile
Returns a new instance of Logfile.
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
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
3 4 5 |
# File 'lib/logfile_interval/logfile.rb', line 3 def filename @filename end |
#parser ⇒ Object (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_line ⇒ Object
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_line ⇒ Object 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
15 16 17 |
# File 'lib/logfile_interval/logfile.rb', line 15 def exist? filename && File.exist?(@filename) end |
#first_parsed_line ⇒ Object 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_timestamp ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/logfile_interval/logfile.rb', line 19 def 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 |