Class: TypedBus::Stats

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



9
10
11
# File 'lib/typed_bus/stats.rb', line 9

def initialize
  @data = {}
end

Instance Attribute Details

#dataObject (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.

Parameters:

  • key (Symbol)

Returns:

  • (Integer)


23
24
25
# File 'lib/typed_bus/stats.rb', line 23

def [](key)
  @data[key] || 0
end

#increment(key) ⇒ Integer

Increment a named counter.

Parameters:

  • key (Symbol)

Returns:

  • (Integer)

    new value



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_hHash<Symbol, Integer>

Return a snapshot of all counters.

Returns:

  • (Hash<Symbol, Integer>)


34
35
36
# File 'lib/typed_bus/stats.rb', line 34

def to_h
  @data.dup
end