sum_sum 0.0.3 → 0.0.4
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.
- data/CHANGES.md +8 -1
- data/Gemfile.lock +1 -1
- data/lib/sum_sum.rb +8 -3
- data/lib/sum_sum/version.rb +1 -1
- data/spec/sum_sum_spec.rb +135 -137
- metadata +2 -2
data/CHANGES.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
### dev
|
2
2
|
|
3
|
-
[full changelog](http://github.com/yolk/sum_sum/compare/v0.0.
|
3
|
+
[full changelog](http://github.com/yolk/sum_sum/compare/v0.0.4...master)
|
4
|
+
|
5
|
+
### 0.0.4 / 2011-01-27
|
6
|
+
|
7
|
+
[full changelog](http://github.com/yolk/sum_sum/compare/v0.0.3...v0.0.4)
|
8
|
+
|
9
|
+
* Fixed major bug on #sort!
|
10
|
+
* Some pretty_print support
|
4
11
|
|
5
12
|
### 0.0.3 / 2011-01-27
|
6
13
|
|
data/Gemfile.lock
CHANGED
data/lib/sum_sum.rb
CHANGED
@@ -24,9 +24,9 @@ class SumSum < Hash
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def sort!
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
return self if bottom?
|
28
|
+
values.each(&:sort!)
|
29
|
+
to_a.sort_by{|it| it[1].count}.reverse.tap do |array|
|
30
30
|
clear
|
31
31
|
array.each{|k, v| self[k] = v }
|
32
32
|
end
|
@@ -45,6 +45,11 @@ class SumSum < Hash
|
|
45
45
|
bottom? ? "#{count}" : "{#{name}:#{count} #{super.gsub(/^\{|\}$/, "")}}"
|
46
46
|
end
|
47
47
|
|
48
|
+
def pretty_print(pp)
|
49
|
+
return pp.text(" #{count}") if bottom?
|
50
|
+
super
|
51
|
+
end
|
52
|
+
|
48
53
|
def dump
|
49
54
|
return count if bottom?
|
50
55
|
hash = {}
|
data/lib/sum_sum/version.rb
CHANGED
data/spec/sum_sum_spec.rb
CHANGED
@@ -15,168 +15,166 @@ describe SumSum do
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
let(:sum) { SumSum.new(:type, :name, :version) }
|
19
|
+
|
20
|
+
context "adding single hash" do
|
21
|
+
before do
|
22
|
+
sum.add({:type => :Browser, :name => :Firefox, :version => "3.6.0"})
|
23
|
+
end
|
20
24
|
|
21
|
-
context "
|
22
|
-
|
23
|
-
sum.
|
25
|
+
context "#count" do
|
26
|
+
it "should return 1 on all levels" do
|
27
|
+
sum.count.should eql(1)
|
28
|
+
sum[:Browser].count.should eql(1)
|
29
|
+
sum[:Browser][:Firefox].count.should eql(1)
|
30
|
+
sum[:Browser][:Firefox]["3.6.0"].count.should eql(1)
|
24
31
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
32
|
+
end
|
33
|
+
|
34
|
+
context "#keys" do
|
35
|
+
it "should return correct keys on all levels" do
|
36
|
+
sum.keys.should eql([:Browser])
|
37
|
+
sum[:Browser].keys.should eql([:Firefox])
|
38
|
+
sum[:Browser][:Firefox].keys.should eql(["3.6.0"])
|
39
|
+
sum[:Browser][:Firefox]["3.6.0"].keys.should eql([])
|
33
40
|
end
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
41
|
+
end
|
42
|
+
|
43
|
+
context "#dump" do
|
44
|
+
it "should output serializable array" do
|
45
|
+
sum.dump.should eql(
|
46
|
+
[[:type, :name, :version], {:Browser=>{:Firefox=>{"3.6.0"=>1}}}]
|
47
|
+
)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context ".load" do
|
52
|
+
it "should restore everything from serializable array" do
|
53
|
+
SumSum.load(sum.dump).dump.should eql(sum.dump)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "adding multiple hashes" do
|
59
|
+
before do
|
60
|
+
sum.add({:type => :Crawler, :name => :GoogleBot, :version => "2.0"})
|
61
|
+
sum.add({:type => :Browser, :name => :Firefox, :version => "3.6.0"})
|
62
|
+
sum.add({:type => :Browser, :name => :Safari, :version => "4.0"})
|
63
|
+
sum.add({:type => :Browser, :name => :Safari, :version => "5.0"})
|
64
|
+
sum.add({:type => :Browser, :name => :Safari, :version => "5.0"})
|
65
|
+
end
|
66
|
+
|
67
|
+
context "#add" do
|
68
|
+
it "should return itself" do
|
69
|
+
sum.add({:type => :Crawler, :name => :GoogleBot, :version => "2.0"}).should eql(sum)
|
42
70
|
end
|
43
71
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
72
|
+
it "should add missing values as nil" do
|
73
|
+
sum.add({:type => :Crawler, :name => nil})
|
74
|
+
sum.add({:type => :Crawler})
|
75
|
+
|
76
|
+
sum[:Crawler].keys.should eql([:GoogleBot, nil])
|
77
|
+
sum[:Crawler][nil].count.should eql(2)
|
78
|
+
sum[:Crawler][nil].keys.should eql([nil])
|
79
|
+
sum[:Crawler][nil][nil].count.should eql(2)
|
50
80
|
end
|
51
81
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
82
|
+
it "should allow to add multiple counts at once" do
|
83
|
+
sum.add({:type => :Crawler, :name => :DuckDuckGo, :version => "1.0"}, 10)
|
84
|
+
|
85
|
+
sum[:Crawler].count.should eql(11)
|
86
|
+
sum[:Crawler][:DuckDuckGo].count.should eql(10)
|
87
|
+
sum[:Crawler][:DuckDuckGo]["1.0"].count.should eql(10)
|
56
88
|
end
|
57
89
|
end
|
58
90
|
|
59
|
-
context "
|
60
|
-
|
61
|
-
sum.
|
62
|
-
sum.add({:type => :Browser, :name => :Firefox, :version => "3.6.0"})
|
63
|
-
sum.add({:type => :Browser, :name => :Safari, :version => "4.0"})
|
64
|
-
sum.add({:type => :Browser, :name => :Safari, :version => "5.0"})
|
65
|
-
sum.add({:type => :Browser, :name => :Safari, :version => "5.0"})
|
66
|
-
end
|
67
|
-
|
68
|
-
context "#add" do
|
69
|
-
it "should return itself" do
|
70
|
-
sum.add({:type => :Crawler, :name => :GoogleBot, :version => "2.0"}).should eql(sum)
|
71
|
-
end
|
91
|
+
context "#count" do
|
92
|
+
it "should return correct count on all levels" do
|
93
|
+
sum.count.should eql(5)
|
72
94
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
sum[:Crawler][nil].keys.should eql([nil])
|
80
|
-
sum[:Crawler][nil][nil].count.should eql(2)
|
81
|
-
end
|
95
|
+
sum[:Browser].count.should eql(4)
|
96
|
+
sum[:Browser][:Firefox].count.should eql(1)
|
97
|
+
sum[:Browser][:Firefox]["3.6.0"].count.should eql(1)
|
98
|
+
sum[:Browser][:Safari].count.should eql(3)
|
99
|
+
sum[:Browser][:Safari]["5.0"].count.should eql(2)
|
100
|
+
sum[:Browser][:Safari]["4.0"].count.should eql(1)
|
82
101
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
sum[:Crawler].count.should eql(11)
|
87
|
-
sum[:Crawler][:DuckDuckGo].count.should eql(10)
|
88
|
-
sum[:Crawler][:DuckDuckGo]["1.0"].count.should eql(10)
|
89
|
-
end
|
102
|
+
sum[:Crawler].count.should eql(1)
|
103
|
+
sum[:Crawler][:GoogleBot].count.should eql(1)
|
104
|
+
sum[:Crawler][:GoogleBot]["2.0"].count.should eql(1)
|
90
105
|
end
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
sum[:Crawler][:GoogleBot]["2.0"].count.should eql(1)
|
106
|
-
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "#keys" do
|
109
|
+
it "should return correct keys on all levels" do
|
110
|
+
sum.keys.should eql([:Crawler, :Browser])
|
111
|
+
sum[:Browser].keys.should eql([:Firefox, :Safari])
|
112
|
+
sum[:Browser][:Firefox].keys.should eql(["3.6.0"])
|
113
|
+
sum[:Browser][:Firefox]["3.6.0"].keys.should eql([])
|
114
|
+
sum[:Browser][:Safari].keys.should eql(["4.0", "5.0"])
|
115
|
+
sum[:Browser][:Safari]["4.0"].keys.should eql([])
|
116
|
+
sum[:Browser][:Safari]["5.0"].keys.should eql([])
|
117
|
+
sum[:Crawler].keys.should eql([:GoogleBot])
|
118
|
+
sum[:Crawler][:GoogleBot].keys.should eql(["2.0"])
|
119
|
+
sum[:Crawler][:GoogleBot]["2.0"].keys.should eql([])
|
107
120
|
end
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
sum[:Browser][:Safari].keys.should eql(["4.0", "5.0"])
|
116
|
-
sum[:Browser][:Safari]["4.0"].keys.should eql([])
|
117
|
-
sum[:Browser][:Safari]["5.0"].keys.should eql([])
|
118
|
-
sum[:Crawler].keys.should eql([:GoogleBot])
|
119
|
-
sum[:Crawler][:GoogleBot].keys.should eql(["2.0"])
|
120
|
-
sum[:Crawler][:GoogleBot]["2.0"].keys.should eql([])
|
121
|
-
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context "#sort" do
|
124
|
+
before do
|
125
|
+
sum.add({:type => :Browser, :name => :Safari, :version => "5.0.3"}, 3)
|
126
|
+
sum.add({:type => :Browser, :name => :Chrome, :version => "5.0"}, 2)
|
127
|
+
sum.sort!
|
122
128
|
end
|
123
129
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
130
|
+
it "should sort by count" do
|
131
|
+
sum.keys.should eql([:Browser, :Crawler])
|
132
|
+
sum[:Browser].keys.should eql([:Safari, :Chrome, :Firefox])
|
133
|
+
sum[:Browser][:Safari].keys.should eql(["5.0.3", "5.0", "4.0"])
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
context "#share" do
|
138
|
+
it "should return 1.0 on root" do
|
139
|
+
sum.share.should eql(1.0)
|
134
140
|
end
|
135
141
|
|
136
|
-
|
137
|
-
|
138
|
-
sum.share.should eql(1.0)
|
139
|
-
end
|
140
|
-
|
141
|
-
it "should return 1.0 on branch with single entry" do
|
142
|
-
sum[:Crawler][:GoogleBot].share.should eql(1.0)
|
143
|
-
end
|
144
|
-
|
145
|
-
it "should return 0.2 on branch with one out of five" do
|
146
|
-
sum[:Crawler].share.should eql(0.2)
|
147
|
-
end
|
148
|
-
|
149
|
-
it "should return 0.8 on branch with four out of five" do
|
150
|
-
sum[:Browser].share.should eql(0.8)
|
151
|
-
end
|
152
|
-
|
153
|
-
it "should return 0.25 on branch with one out of four" do
|
154
|
-
sum[:Browser][:Firefox].share.should eql(0.25)
|
155
|
-
end
|
142
|
+
it "should return 1.0 on branch with single entry" do
|
143
|
+
sum[:Crawler][:GoogleBot].share.should eql(1.0)
|
156
144
|
end
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
sum.dump.should eql(
|
161
|
-
[[:type, :name, :version], {
|
162
|
-
:Crawler=>{:GoogleBot=>{"2.0"=>1}},
|
163
|
-
:Browser=>{
|
164
|
-
:Firefox=>{"3.6.0"=>1},
|
165
|
-
:Safari=>{"4.0"=>1, "5.0"=>2}
|
166
|
-
}
|
167
|
-
}]
|
168
|
-
)
|
169
|
-
end
|
145
|
+
|
146
|
+
it "should return 0.2 on branch with one out of five" do
|
147
|
+
sum[:Crawler].share.should eql(0.2)
|
170
148
|
end
|
171
149
|
|
172
|
-
|
173
|
-
|
174
|
-
SumSum.load(sum.dump).dump.should eql(sum.dump)
|
175
|
-
end
|
150
|
+
it "should return 0.8 on branch with four out of five" do
|
151
|
+
sum[:Browser].share.should eql(0.8)
|
176
152
|
end
|
177
153
|
|
154
|
+
it "should return 0.25 on branch with one out of four" do
|
155
|
+
sum[:Browser][:Firefox].share.should eql(0.25)
|
156
|
+
end
|
178
157
|
end
|
179
158
|
|
159
|
+
context "#dump" do
|
160
|
+
it "should output serializable array" do
|
161
|
+
sum.dump.should eql(
|
162
|
+
[[:type, :name, :version], {
|
163
|
+
:Crawler=>{:GoogleBot=>{"2.0"=>1}},
|
164
|
+
:Browser=>{
|
165
|
+
:Firefox=>{"3.6.0"=>1},
|
166
|
+
:Safari=>{"4.0"=>1, "5.0"=>2}
|
167
|
+
}
|
168
|
+
}]
|
169
|
+
)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context ".load" do
|
174
|
+
it "should restore everything from serializable array" do
|
175
|
+
SumSum.load(sum.dump).dump.should eql(sum.dump)
|
176
|
+
end
|
177
|
+
end
|
180
178
|
|
181
179
|
end
|
182
180
|
end
|