trick_bag 0.49.0 → 0.50.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/RELEASE_NOTES.md +5 -0
- data/lib/trick_bag/numeric/multi_counter.rb +20 -4
- data/lib/trick_bag/version.rb +1 -1
- data/spec/trick_bag/numeric/multi_counter_spec.rb +7 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18fca0df6bf55045308d1addceafea1793c14b87
|
4
|
+
data.tar.gz: 37c89c74b67277d29d959d10a0d17636c6347269
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa4fa5ddde0bca6c34ef27af74122b1ae1b669597b135f674b0f8e3dfac812a0d69576cea742ca2fe27ef5473cc1895ff5f046f1da0c36ec7d1104cfb2e5365c
|
7
|
+
data.tar.gz: 377ec62488029e12b522af9e8cc0ec7ee49bdd54028cc85c916b9d4da5c3f7b10ad99ef7e67714c3e17217401a8f6476d7213b056f6e9886f8ab74bd542ac88f
|
data/RELEASE_NOTES.md
CHANGED
@@ -15,6 +15,7 @@ class MultiCounter
|
|
15
15
|
@counts = Hash.new(0)
|
16
16
|
end
|
17
17
|
|
18
|
+
# Creates an instance and iterates over the enumberable, processing all its items.
|
18
19
|
def self.from_enumerable(values, name = '')
|
19
20
|
m_counter = MultiCounter.new(name)
|
20
21
|
values.each { |value| m_counter.increment(value) }
|
@@ -51,13 +52,28 @@ class MultiCounter
|
|
51
52
|
@counts.values.inject(0, &:+)
|
52
53
|
end
|
53
54
|
|
54
|
-
#
|
55
|
-
#
|
56
|
-
|
55
|
+
# Private internal method for use by percent_of_total_hash
|
56
|
+
# and fraction_of_total_hash.
|
57
|
+
# @param mode :percent or :fraction
|
58
|
+
def of_total_hash(mode = :percent)
|
59
|
+
raise "bad mode: #{mode}" unless [:percent, :fraction].include?(mode)
|
57
60
|
total = total_count
|
58
61
|
keys.each_with_object({}) do |key, ptotal_hash|
|
59
|
-
|
62
|
+
value = Float(self[key]) / total
|
63
|
+
value *= 100 if mode == :percent
|
64
|
+
ptotal_hash[key] = value
|
60
65
|
end
|
66
|
+
end; private :of_total_hash
|
67
|
+
|
68
|
+
# Returns a hash whose keys are the multicounter's keys and whose values
|
69
|
+
# are the percent of total of the values corresponding to those keys.
|
70
|
+
def percent_of_total_hash
|
71
|
+
of_total_hash(:percent)
|
72
|
+
end
|
73
|
+
# Returns a hash whose keys are the multicounter's keys and whose values
|
74
|
+
# are the fraction of total of the values corresponding to those keys.
|
75
|
+
def fraction_of_total_hash
|
76
|
+
of_total_hash(:fraction)
|
61
77
|
end
|
62
78
|
|
63
79
|
# Creates a hash whose keys are this counter's keys, and whose values are
|
data/lib/trick_bag/version.rb
CHANGED
@@ -38,6 +38,13 @@ module Numeric
|
|
38
38
|
|
39
39
|
specify 'percent_of_total_hash is correctly calculated' do
|
40
40
|
ptotal_hash = MultiCounter.from_enumerable(sample_data).percent_of_total_hash
|
41
|
+
expect(ptotal_hash['Open']).to eq(60.0)
|
42
|
+
expect(ptotal_hash['Closed']).to eq(20.0)
|
43
|
+
expect(ptotal_hash['New']).to eq(20.0)
|
44
|
+
end
|
45
|
+
|
46
|
+
specify 'fraction_of_total_hash is correctly calculated' do
|
47
|
+
ptotal_hash = MultiCounter.from_enumerable(sample_data).fraction_of_total_hash
|
41
48
|
expect(ptotal_hash['Open']).to eq(0.6)
|
42
49
|
expect(ptotal_hash['Closed']).to eq(0.2)
|
43
50
|
expect(ptotal_hash['New']).to eq(0.2)
|