Class: Tap::Support::Aggregator
- Defined in:
- lib/tap/support/aggregator.rb
Overview
Aggregator allows thread-safe collection of Audits, organized by Audit#_current_source.
a = Audit.new
a._record(:src, 'a')
b = Audit.new
b._record(:src, 'b')
agg = Aggregator.new
agg.store(a)
agg.store(b)
agg.retrieve(:src) # => [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._current_source.
-
#to_hash ⇒ Object
Converts self to a hash of (source, audits) pairs.
Constructor Details
#initialize ⇒ Aggregator
Creates a new Aggregator.
21 22 23 24 |
# File 'lib/tap/support/aggregator.rb', line 21 def initialize super clear end |
Instance Method Details
#clear ⇒ Object
Clears self of all audits.
27 28 29 |
# File 'lib/tap/support/aggregator.rb', line 27 def clear synchronize { self.hash = Hash.new } end |
#empty? ⇒ Boolean
True if size == 0
37 38 39 |
# File 'lib/tap/support/aggregator.rb', line 37 def empty? synchronize { hash.empty? } end |
#retrieve(source) ⇒ Object
Retreives all aggregated audits for the specified source.
47 48 49 |
# File 'lib/tap/support/aggregator.rb', line 47 def retrieve(source) synchronize { hash[source] } end |
#retrieve_all(*sources) ⇒ Object
Retreives all audits for the input sources, joined as an array.
52 53 54 55 56 |
# File 'lib/tap/support/aggregator.rb', line 52 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.
32 33 34 |
# File 'lib/tap/support/aggregator.rb', line 32 def size synchronize { hash.values.inject(0) {|sum, array| sum + array.length} } end |
#store(_result) ⇒ Object
Stores the Audit according to _result._current_source
42 43 44 |
# File 'lib/tap/support/aggregator.rb', line 42 def store(_result) synchronize { (hash[_result._current_source] ||= []) << _result } end |
#to_hash ⇒ Object
Converts self to a hash of (source, audits) pairs.
59 60 61 |
# File 'lib/tap/support/aggregator.rb', line 59 def to_hash hash.dup end |