class Tailstrom::TailReader

Public Class Methods

new(infile, options={}) click to toggle source
# File lib/tailstrom/tail_reader.rb, line 3
def initialize(infile, options={})
  @infile = infile
  @options = options
end

Public Instance Methods

each_line(&block) click to toggle source
# 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?() click to toggle source
# File lib/tailstrom/tail_reader.rb, line 27
def eof?
  @eof
end
format_value(value) click to toggle source
# File lib/tailstrom/tail_reader.rb, line 61
def format_value(value)
  value =~ /\./ ? value.to_f : value.to_i
end
parse_line(line) click to toggle source
# 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

Private Instance Methods

_each_line() { |result| ... } click to toggle source
# File lib/tailstrom/tail_reader.rb, line 16
def _each_line
  @eof = false
  @infile.each_line do |line|
    line.chomp!
    result = parse_line(line)
    yield result if result
  end
  @eof = true
end
index_or_eval(var, col, idx) click to toggle source
# File lib/tailstrom/tail_reader.rb, line 65
def index_or_eval(var, col, idx)
  case idx
  when Integer
    "#{var}=col[#{idx}]"
  when String
    "#{var}=#{idx}"
  end
end