Class: Tailstrom::TailReader

Inherits:
Object
  • Object
show all
Defined in:
lib/tailstrom/tail_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(infile, options = {}) ⇒ TailReader

Returns a new instance of TailReader.



3
4
5
6
# File 'lib/tailstrom/tail_reader.rb', line 3

def initialize(infile, options={})
  @infile = infile
  @options = options
end

Instance Method Details

#each_line(&block) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/tailstrom/tail_reader.rb', line 8

def each_line(&block)
  if @options[:async]
    Thread.new { _each_line &block }
  else
    _each_line &block
  end
end

#eof?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/tailstrom/tail_reader.rb', line 27

def eof?
  @eof
end

#format_value(value) ⇒ Object



61
62
63
# File 'lib/tailstrom/tail_reader.rb', line 61

def format_value(value)
  value =~ /\./ ? value.to_f : value.to_i
end

#parse_line(line) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tailstrom/tail_reader.rb', line 31

def parse_line(line)
  col = line.split @options[:delimiter]
  key = value = nil
  in_filter = @options[:in_filter]

  _scripts = []

  if @options[:map]
    _scripts << @options[:map]
    _scripts << 'value=format_value(value)'
  end

  if @options[:key]
    _scripts << index_or_eval('key', 'col', @options[:key])
  end

  if @options[:value]
    _scripts << index_or_eval('value', 'col', @options[:value])
    _scripts << 'value=format_value(value)'
  end

  if in_filter
    _scripts << in_filter
    return nil unless eval _scripts.join(';')
  end

  eval _scripts.join(';')
  { :line => line, :columns => col, :key => key, :value => value }
end