Sha256: a43c1fbcf4de74f4233825f0523cbaee048196fa6089d06eb6cda26cba1eba53

Contents?: true

Size: 895 Bytes

Versions: 1

Compression:

Stored size: 895 Bytes

Contents

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

    def each_line
      @infile.each_line do |line|
        line.chomp!
        result = parse_line(line)
        yield result if result
      end
    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
      filter = @options[:filter]

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

      if filter
        return nil unless binding.eval(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

1 entries across 1 versions & 1 rubygems

Version Path
tailstrom-0.0.4 lib/tailstrom/tail_reader.rb