Class: TrickBag::Numeric::MultiCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/trick_bag/numeric/multi_counter.rb

Overview

Simplifies accumulating counts of multiple objects. Like a hash, but does not allow []=; increment is the only way to modify a value.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = '') ⇒ MultiCounter

Creates a multicounter.

Parameters:

  • name (defaults to: '')

    optional name for printing in to_s



13
14
15
16
# File 'lib/trick_bag/numeric/multi_counter.rb', line 13

def initialize(name = '')
  @name = name
  @counts = Hash.new(0)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/trick_bag/numeric/multi_counter.rb', line 8

def name
  @name
end

Class Method Details

.from_enumerable(values, name = '') ⇒ Object

Creates an instance and iterates over the enumberable, processing all its items.



19
20
21
22
23
# File 'lib/trick_bag/numeric/multi_counter.rb', line 19

def self.from_enumerable(values, name = '')
  m_counter = MultiCounter.new(name)
  values.each { |value| m_counter.increment(value) }
  m_counter
end

Instance Method Details

#[](key) ⇒ Object

Returns the count for the specified key.



36
37
38
# File 'lib/trick_bag/numeric/multi_counter.rb', line 36

def [](key)
  @counts[key]
end

#add_keys(keys) ⇒ Object

Adds keys in the passed enumerable to this counter.



26
27
28
# File 'lib/trick_bag/numeric/multi_counter.rb', line 26

def add_keys(keys)
  keys.each { |key| @counts[key] = 0 }
end

#fraction_of_total_hashObject

Returns a hash whose keys are the multicounter’s keys and whose values are the fraction of total of the values corresponding to those keys.



75
76
77
# File 'lib/trick_bag/numeric/multi_counter.rb', line 75

def fraction_of_total_hash
  of_total_hash(:fraction)
end

#increment(key) ⇒ Object

Increments the value corresponding to the specified key.



31
32
33
# File 'lib/trick_bag/numeric/multi_counter.rb', line 31

def increment(key)
  @counts[key] += 1
end

#key_exists?(key) ⇒ Boolean

Returns whether or not the specified key exists in this counter.

Returns:

  • (Boolean)


46
47
48
# File 'lib/trick_bag/numeric/multi_counter.rb', line 46

def key_exists?(key)
  keys.include?(key)
end

#keysObject

Returns this counter’s keys.



41
42
43
# File 'lib/trick_bag/numeric/multi_counter.rb', line 41

def keys
  @counts.keys
end

#percent_of_total_hashObject

Returns a hash whose keys are the multicounter’s keys and whose values are the percent of total of the values corresponding to those keys.



70
71
72
# File 'lib/trick_bag/numeric/multi_counter.rb', line 70

def percent_of_total_hash
  of_total_hash(:percent)
end

#to_hashObject

Creates a hash whose keys are this counter’s keys, and whose values are their corresponding values.



81
82
83
# File 'lib/trick_bag/numeric/multi_counter.rb', line 81

def to_hash
  @counts.clone
end

#to_sObject

Returns a string representing this counter, including its values, and, if specified, its optional name.



87
88
89
# File 'lib/trick_bag/numeric/multi_counter.rb', line 87

def to_s
  "#{self.class} '#{name}': #{@counts.to_s}"
end

#total_countObject

Returns the total of all counts.



51
52
53
# File 'lib/trick_bag/numeric/multi_counter.rb', line 51

def total_count
  @counts.values.inject(0, &:+)
end