Class: TypedBus::Stats
- Inherits:
-
Object
- Object
- TypedBus::Stats
- Defined in:
- lib/typed_bus/stats.rb
Overview
Simple counter map for message bus activity. Fiber-safe — no mutex needed within a single Async reactor.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#[](key) ⇒ Integer
Read a counter value.
-
#increment(key) ⇒ Integer
Increment a named counter.
-
#initialize ⇒ Stats
constructor
A new instance of Stats.
-
#reset! ⇒ Object
Reset all counters to zero.
-
#to_h ⇒ Hash<Symbol, Integer>
Return a snapshot of all counters.
Constructor Details
#initialize ⇒ Stats
Returns a new instance of Stats.
9 10 11 |
# File 'lib/typed_bus/stats.rb', line 9 def initialize @data = {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/typed_bus/stats.rb', line 7 def data @data end |
Instance Method Details
#[](key) ⇒ Integer
Read a counter value.
23 24 25 |
# File 'lib/typed_bus/stats.rb', line 23 def [](key) @data[key] || 0 end |
#increment(key) ⇒ Integer
Increment a named counter.
16 17 18 |
# File 'lib/typed_bus/stats.rb', line 16 def increment(key) @data[key] = (@data[key] || 0) + 1 end |
#reset! ⇒ Object
Reset all counters to zero.
28 29 30 |
# File 'lib/typed_bus/stats.rb', line 28 def reset! @data.transform_values! { 0 } end |
#to_h ⇒ Hash<Symbol, Integer>
Return a snapshot of all counters.
34 35 36 |
# File 'lib/typed_bus/stats.rb', line 34 def to_h @data.dup end |