class Elected::Stats

Attributes

counters[R]

Public Class Methods

new(*names) click to toggle source
# File lib/elected/stats.rb, line 9
def initialize(*names)
  @counters = Concurrent::Hash.new
  names.map { |x| get_or_set_counter x }
end

Public Instance Methods

count(name) click to toggle source
# File lib/elected/stats.rb, line 14
def count(name)
  get_or_set_counter(name).sum
end
increment(name, value = 1) click to toggle source
# File lib/elected/stats.rb, line 18
def increment(name, value = 1)
  get_or_set_counter(name).add value
  count name
end
inspect()
Alias for: to_s
to_hash() click to toggle source
# File lib/elected/stats.rb, line 23
def to_hash
  @counters.keys.sort.each_with_object({}) { |k, h| h[k] = count k }
end
to_s() click to toggle source
# File lib/elected/stats.rb, line 27
def to_s
  %{#<#{self.class.name} #{to_hash.map { |k, v| "#{k}=#{v}" }.join(' ')}>}
end
Also aliased as: inspect

Private Instance Methods

get_or_set_counter(name, initial_value = 0) click to toggle source
# File lib/elected/stats.rb, line 35
def get_or_set_counter(name, initial_value = 0)
  safe_name = name.to_s.strip.underscore.to_sym
  found     = @counters[safe_name]
  return found if found

  @counters[safe_name] = Concurrent::ThreadSafe::Util::Adder.new.tap do |c|
    c.add(initial_value) unless initial_value == 0
  end
end