Sha256: 9c8326fe4aad75e22e085d41a2da9fe7f8f01818e5354cfb0299faaa3a984027

Contents?: true

Size: 867 Bytes

Versions: 1

Compression:

Stored size: 867 Bytes

Contents

module LogfileInterval
  module LineParser
    class Counter < Hash
      def increment(key)
        if self.has_key?(key)
          self[key] += 1
        else
          self[key] = 1
        end
      end

      def increment_subkey(key, subkey)
        if self.has_key?(key) && !self[key].is_a?(Counter)
          raise "Value for #{key} is not a Counter"
        end

        unless self.has_key?(key)
          self[key] = Counter.new
        end
        self[key].increment(subkey)
      end

      def add(key, num)
        if self.has_key?(key)
          self[key] += num
        else
          self[key] = num
        end
      end

      def set(key, num)
        self[key] = num
      end

      def [](key)
        self.fetch(key, 0)
      end

      def merge(c)
        c.keys.each do |k|
          self.add c[k]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logfile_interval-1.1.1 lib/logfile_interval/line_parser/counter.rb