Class: Tap::Support::Aggregator

Inherits:
Monitor
  • Object
show all
Defined in:
lib/tap/support/aggregator.rb

Overview

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]

Instance Method Summary collapse

Constructor Details

#initializeAggregator

Creates a new Aggregator.



18
19
20
21
# File 'lib/tap/support/aggregator.rb', line 18

def initialize
  super
  clear
end

Instance Method Details

#clearObject

Clears self of all audits.



24
25
26
# File 'lib/tap/support/aggregator.rb', line 24

def clear
  synchronize { self.hash = Hash.new }
end

#empty?Boolean

True if size == 0

Returns:

  • (Boolean)


34
35
36
# File 'lib/tap/support/aggregator.rb', line 34

def empty?
  synchronize { hash.empty? }
end

#retrieve(source) ⇒ Object

Retreives all aggregated audits for the specified source.



44
45
46
# File 'lib/tap/support/aggregator.rb', line 44

def retrieve(source)
  synchronize { hash[source] }
end

#retrieve_all(*sources) ⇒ Object

Retreives all audits for the input sources, joined as an array.



49
50
51
52
53
# File 'lib/tap/support/aggregator.rb', line 49

def retrieve_all(*sources)
  synchronize do
    sources.collect {|src| hash[src] }.flatten.compact
  end
end

#sizeObject

The total number of audits recorded in self.



29
30
31
# File 'lib/tap/support/aggregator.rb', line 29

def size
  synchronize { hash.values.inject(0) {|sum, array| sum + array.length} }
end

#store(_result) ⇒ Object

Stores the Audit according to _result.key



39
40
41
# File 'lib/tap/support/aggregator.rb', line 39

def store(_result)
  synchronize { (hash[_result.key] ||= []) << _result }
end

#to_hashObject

Converts self to a hash of (source, audits) pairs.



56
57
58
# File 'lib/tap/support/aggregator.rb', line 56

def to_hash
  hash.dup
end