Sha256: 8d4a8f28a595d2f801ac1fbbb5df8a7e01a9fa870fcffb74d250bd025ff0ec7a

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

module Tap
  module Support
  
    # Aggregator allows thread-safe collection of Audits, organized by
    # Audit#key.
    #
    #   a = Audit.new(:key, 'a')
    #   b = Audit.new(:key, 'b')
    #
    #   agg = Aggregator.new
    #   agg.store(a)
    #   agg.store(b)
    #   agg.retrieve(:key)             # => [a, b]
    #
    class Aggregator < Monitor
      
      # Creates a new Aggregator.
      def initialize
        super
        @hash = {}
      end
      
      # Clears self of all audits. Returns the existing audits as a hash
      # of (key, audits) pairs.
      def clear
        synchronize do
          current, @hash = @hash, {}
          current
        end
      end
      
      # The total number of audits recorded in self.
      def size
        synchronize { @hash.values.inject(0) {|sum, array| sum + array.length} }
      end
      
      # True if size == 0
      def empty?
        synchronize { size == 0 }
      end
      
      # Stores the Audit according to _audit.key.
      def store(_audit)
        synchronize { (@hash[_audit.key] ||= []) << _audit }
      end
      
      # Retreives all audits for the specified key.
      def retrieve(key)
        synchronize { @hash[key] }
      end
      
      # Retreives all audits for the input keys, joined as an array.
      def retrieve_all(*keys)
        synchronize do
          keys.collect {|src| @hash[src] }.flatten.compact
        end
      end
      
      # Converts self to a hash of (key, audits) pairs.
      def to_hash
        synchronize { @hash.dup }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tap-0.12.1 lib/tap/support/aggregator.rb
tap-0.12.4 lib/tap/support/aggregator.rb
tap-0.12.2 lib/tap/support/aggregator.rb
tap-0.12.3 lib/tap/support/aggregator.rb
tap-0.12.0 lib/tap/support/aggregator.rb