File: counter.rb

package info (click to toggle)
ruby-metriks 0.9.9.8-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: ruby: 1,877; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 876 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'atomic'

module Metriks
  # Public: Counters are one of the simplest metrics whose only operations
  # are increment and decrement.
  class Counter
    # Public: Initialize a new Counter.
    def initialize
      @count = Atomic.new(0)
    end

    # Public: Reset the counter back to 0
    #
    # Returns nothing.
    def clear
      @count.value = 0
    end

    # Public: Increment the counter.
    #
    # incr - The value to add to the counter.
    #
    # Returns nothing.
    def increment(incr = 1)
      @count.update { |v| v + incr }
    end

    # Public: Decrement the counter.
    #
    # decr - The value to subtract from the counter.
    #
    # Returns nothing.
    def decrement(decr = 1)
      @count.update { |v| v - decr }
    end

    # Public: The current count.
    #
    # Returns the count.
    def count
      @count.value
    end
  end
end