Sha256: 3ef1abd18d6bb80fc2edbc82ac4e3916b8b260fc74ff3a7905f79f5a94257295

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module VCLog

  #
  class Heuristics

    # Load heuristics from a configruation file.
    #
    # config - the configuration directory
    #
    def self.load(file)
      if file
        raise LoadError unless File.exist?(file)
      else
        file = File.dirname(__FILE__) + '/heuristics/default.rb'
      end

      h = new
      h.instance_eval(File.read(file), file)
      h
    end

    #
    def initialize
      @rules  = []
      @labels = {}
    end

    #
    def lookup(message)
      type = nil
      @rules.find{ |rule| type = rule.call(message) }
      if type
        type = type.to_sym
        if @labels.key?(type)
           @labels[type].to_a
        else
          [type, 0, "#{type.to_s.capitalize} Enhancements"]
        end
      else
        [nil, 0, 'General Enhancements']
      end
    end

    #
    def on(pattern, &block)
      @rules << Rule.new(pattern, &block)
    end

    #
    def set(type, level, label)
      @labels[type.to_sym] = Label.new(type, level, label)
    end

    #
    class Label
      #
      def initialize(type, level, label)
        @type  = type
        @level = level.to_i
        @label = label.to_s
      end

      attr :type

      attr :level

      attr :label

      #
      def to_a
        [type, level, label]
      end
    end

    #
    class Rule
      def initialize(pattern, &block)
        @pattern = pattern
        @block   = block        
      end

      def call(message)
        if md = @pattern.match(message)
          @block.call(*md[1..-1])
        else
          nil
        end
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vclog-1.6.1 lib/vclog/heuristics.rb
vclog-1.6.0 lib/vclog/heuristics.rb