Class: Tap::Support::Aggregator
- 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
-
#clear ⇒ Object
Clears self of all audits.
-
#empty? ⇒ Boolean
True if size == 0.
-
#initialize ⇒ Aggregator
constructor
Creates a new Aggregator.
-
#retrieve(source) ⇒ Object
Retreives all aggregated audits for the specified source.
-
#retrieve_all(*sources) ⇒ Object
Retreives all audits for the input sources, joined as an array.
-
#size ⇒ Object
The total number of audits recorded in self.
-
#store(_result) ⇒ Object
Stores the Audit according to _result.key.
-
#to_hash ⇒ Object
Converts self to a hash of (source, audits) pairs.
Constructor Details
#initialize ⇒ Aggregator
Creates a new Aggregator.
18 19 20 21 |
# File 'lib/tap/support/aggregator.rb', line 18 def initialize super clear end |
Instance Method Details
#clear ⇒ Object
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
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 |
#size ⇒ Object
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_hash ⇒ Object
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 |