Class: Solanum::Matcher::LinePattern

Inherits:
Solanum::Matcher show all
Defined in:
lib/solanum/matcher.rb

Overview

LinePattern matchers define a regular expression which is tested against each line of input. The given function is called for each matched line, and the resulting measurements are merged together.

Instance Attribute Summary

Attributes inherited from Solanum::Matcher

#fn

Instance Method Summary collapse

Constructor Details

#initialize(fn, pattern) ⇒ LinePattern

Returns a new instance of LinePattern.



30
31
32
33
34
# File 'lib/solanum/matcher.rb', line 30

def initialize(fn, pattern)
  super fn
  raise "pattern must be provided" if pattern.nil?
  @pattern = pattern
end

Instance Method Details

#call(input) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/solanum/matcher.rb', line 36

def call(input)
  raise "No input provided!" if input.nil?
  lines = input.split("\n")
  metrics = {}

  lines.each do |line|
    begin
      if @pattern === line
        measurements = @fn.call($~)
        metrics.merge!(measurements) if measurements
      end
    rescue => e
      STDERR.puts("Error calculating metrics from line match: #{e.inspect}")
    end
  end

  metrics
end