Class: Metriks::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/metriks/counter.rb

Overview

Public: Counters are one of the simplest metrics whose only operations are increment and decrement.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCounter

Public: Initialize a new Counter.



10
11
12
# File 'lib/metriks/counter.rb', line 10

def initialize
  @count = Atomic.new(0)
end

Instance Attribute Details

#reset_on_submitObject

sometimes I have multiple processes that count values, so I need to reset every time after submit



8
9
10
# File 'lib/metriks/counter.rb', line 8

def reset_on_submit
  @reset_on_submit
end

Instance Method Details

#activate_reset_on_submitObject

shortcut for onliners



14
15
16
17
# File 'lib/metriks/counter.rb', line 14

def activate_reset_on_submit
  @reset_on_submit = true
  self
end

#clearObject

Public: Reset the counter back to 0

Returns nothing.



21
22
23
# File 'lib/metriks/counter.rb', line 21

def clear
  @count.value = 0
end

#countObject

Public: The current count.

Returns the count.



46
47
48
# File 'lib/metriks/counter.rb', line 46

def count
  @count.value
end

#decrement(decr = 1) ⇒ Object

Public: Decrement the counter.

decr - The value to subtract from the counter.

Returns nothing.



39
40
41
# File 'lib/metriks/counter.rb', line 39

def decrement(decr = 1)
  @count.update { |v| v - decr }
end

#increment(incr = 1) ⇒ Object

Public: Increment the counter.

incr - The value to add to the counter.

Returns nothing.



30
31
32
# File 'lib/metriks/counter.rb', line 30

def increment(incr = 1)
  @count.update { |v| v + incr }
end