Sha256: cfccddb8e3cc8bac2ab064b92c004dae7dff2db0f2060074d05a112b29f2398f

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

module Tailstrom
  class TailReader
    def initialize(infile, options={})
      @infile = infile
      @options = options
    end

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

    def _each_line
      @infile.each_line do |line|
        line.chomp!
        result = parse_line(line)
        yield result if result
      end
    end
    private :_each_line

    def eof?
      @infile.eof?
    end

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

      if @options[:map]
        binding.eval(@options[:map])
        value = format_value value
      end

      if in_filter
        return nil unless binding.eval(in_filter)
      end

      { :line => line, :columns => col, :key => key, :value => value }
    end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tailstrom-0.0.8 lib/tailstrom/tail_reader.rb
tailstrom-0.0.7 lib/tailstrom/tail_reader.rb
tailstrom-0.0.6 lib/tailstrom/tail_reader.rb
tailstrom-0.0.5 lib/tailstrom/tail_reader.rb