progress 0.3.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/progress.rb +2 -13
- data/lib/progress/active_record.rb +27 -0
- data/lib/progress/enumerable.rb +3 -2
- data/lib/progress/kernel.rb +6 -0
- data/progress.gemspec +3 -1
- data/spec/progress_spec.rb +172 -119
- metadata +6 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.1
|
data/lib/progress.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
-
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
-
|
4
1
|
require 'singleton'
|
5
2
|
|
6
3
|
class Progress
|
@@ -179,15 +176,7 @@ class Progress
|
|
179
176
|
end
|
180
177
|
end
|
181
178
|
|
182
|
-
require 'progress/with_progress'
|
183
|
-
|
184
179
|
require 'progress/enumerable'
|
185
180
|
require 'progress/integer'
|
186
|
-
|
187
|
-
|
188
|
-
module Kernel
|
189
|
-
def Progress(title = nil, total = nil, &block)
|
190
|
-
Progress.start(title, total, &block)
|
191
|
-
end
|
192
|
-
private :Progress
|
193
|
-
end
|
181
|
+
require 'progress/kernel'
|
182
|
+
require 'progress/active_record'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
if defined?(ActiveRecord::Base)
|
2
|
+
module ActiveRecord
|
3
|
+
module BatchesWithProgress
|
4
|
+
def find_each_with_progress(options = {})
|
5
|
+
Progress.start(name.tableize, count(options)) do
|
6
|
+
find_each do |model|
|
7
|
+
yield model
|
8
|
+
Progress.step
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def find_in_batches_with_progress(options = {})
|
14
|
+
Progress.start(name.tableize, count(options)) do
|
15
|
+
find_in_batches do |batch|
|
16
|
+
yield batch
|
17
|
+
Progress.step batch.length
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Base
|
24
|
+
extend BatchesWithProgress
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/progress/enumerable.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'enumerator'
|
2
|
+
require 'progress/with_progress'
|
3
|
+
|
2
4
|
module Enumerable
|
3
5
|
# executes any Enumerable method with progress
|
4
6
|
# note that methods which don't necessarily go through all items (like find or any?) will not show 100%
|
@@ -27,8 +29,7 @@ module Enumerable
|
|
27
29
|
# [1, 2, 3].each_with_index_and_progress('Numbers') do |number, index|
|
28
30
|
# sleep(number)
|
29
31
|
# end
|
30
|
-
def each_with_index_and_progress(title, &block)
|
32
|
+
def each_with_index_and_progress(title = nil, &block)
|
31
33
|
with_progress(title).each_with_index(&block)
|
32
34
|
end
|
33
|
-
|
34
35
|
end
|
data/progress.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{progress}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Boba Fat"]
|
@@ -20,8 +20,10 @@ Gem::Specification.new do |s|
|
|
20
20
|
"Rakefile",
|
21
21
|
"VERSION",
|
22
22
|
"lib/progress.rb",
|
23
|
+
"lib/progress/active_record.rb",
|
23
24
|
"lib/progress/enumerable.rb",
|
24
25
|
"lib/progress/integer.rb",
|
26
|
+
"lib/progress/kernel.rb",
|
25
27
|
"lib/progress/with_progress.rb",
|
26
28
|
"progress.gemspec",
|
27
29
|
"spec/progress_spec.rb",
|
data/spec/progress_spec.rb
CHANGED
@@ -22,26 +22,168 @@ describe Progress do
|
|
22
22
|
io_pop.should =~ /100\.0.*\n$/
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
describe 'direct usage' do
|
26
|
+
describe 'procedural' do
|
27
|
+
it "should show valid output when called as Progress.start" do
|
28
|
+
Progress.start('Test', 1000)
|
29
|
+
1000.times do |i|
|
30
|
+
verify_output_before_step(i)
|
31
|
+
Progress.step
|
32
|
+
end
|
33
|
+
Progress.stop
|
34
|
+
verify_output_after_stop
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should show valid output when called as Progress" do
|
38
|
+
Progress('Test', 1000)
|
39
|
+
1000.times do |i|
|
40
|
+
verify_output_before_step(i)
|
41
|
+
Progress.step
|
42
|
+
end
|
43
|
+
Progress.stop
|
44
|
+
verify_output_after_stop
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should show valid output when called without title" do
|
48
|
+
Progress(1000)
|
49
|
+
1000.times do |i|
|
50
|
+
verify_output_before_step(i)
|
51
|
+
Progress.step
|
52
|
+
end
|
53
|
+
Progress.stop
|
54
|
+
verify_output_after_stop
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'block' do
|
59
|
+
it "should show valid output when called as Progress.start" do
|
60
|
+
Progress.start('Test', 1000) do
|
61
|
+
1000.times do |i|
|
62
|
+
verify_output_before_step(i)
|
63
|
+
Progress.step
|
64
|
+
end
|
65
|
+
end
|
66
|
+
verify_output_after_stop
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should show valid output when called as Progress" do
|
70
|
+
Progress('Test', 1000) do
|
71
|
+
1000.times do |i|
|
72
|
+
verify_output_before_step(i)
|
73
|
+
Progress.step
|
74
|
+
end
|
75
|
+
end
|
76
|
+
verify_output_after_stop
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should show valid output when called without title" do
|
80
|
+
Progress(1000) do
|
81
|
+
1000.times do |i|
|
82
|
+
verify_output_before_step(i)
|
83
|
+
Progress.step
|
84
|
+
end
|
85
|
+
end
|
86
|
+
verify_output_after_stop
|
87
|
+
end
|
30
88
|
end
|
31
|
-
Progress.stop
|
32
|
-
verify_output_after_stop
|
33
89
|
end
|
34
90
|
|
35
|
-
|
36
|
-
Progress.
|
37
|
-
|
38
|
-
|
39
|
-
|
91
|
+
describe 'integrity' do
|
92
|
+
it "should not raise errors on extra Progress.stop" do
|
93
|
+
proc{
|
94
|
+
10.times_with_progress('10') do
|
95
|
+
Progress.start 'simple' do
|
96
|
+
Progress.start 'procedural'
|
97
|
+
Progress.stop
|
98
|
+
Progress.stop
|
99
|
+
end
|
100
|
+
Progress.stop
|
101
|
+
end
|
102
|
+
Progress.stop
|
103
|
+
}.should_not raise_error
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should return result from block" do
|
107
|
+
Progress.start('Test') do
|
108
|
+
'qwerty'
|
109
|
+
end.should == 'qwerty'
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should return result from nested block" do
|
113
|
+
[1, 2, 3].with_progress('a').map do |a|
|
114
|
+
[1, 2, 3].with_progress('b').map do |b|
|
115
|
+
a * b
|
116
|
+
end
|
117
|
+
end.should == [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should kill progress on cycle break" do
|
121
|
+
2.times do
|
122
|
+
catch(:lalala) do
|
123
|
+
2.times_with_progress('A') do |a|
|
124
|
+
io_pop.should == "A: ......\n"
|
125
|
+
2.times_with_progress('B') do |b|
|
126
|
+
io_pop.should == "A: ...... > B: ......\n"
|
127
|
+
throw(:lalala)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
io_pop.should == "A: ......\n\n"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
[[2, 2000], [20, 200], [200, 20], [2000, 2]].each do |_a, _b|
|
136
|
+
it "should allow enclosed progress [#{_a}, #{_b}]" do
|
137
|
+
_a.times_with_progress('A') do |a|
|
138
|
+
io_pop.should == "A: #{a == 0 ? '......' : '%5.1f%%'}\n" % [a / _a.to_f * 100.0]
|
139
|
+
_b.times_with_progress('B') do |b|
|
140
|
+
io_pop.should == "A: #{a == 0 && b == 0 ? '......' : '%5.1f%%'} > B: #{b == 0 ? '......' : '%5.1f%%'}\n" % [(a + b / _b.to_f) / _a.to_f * 100.0, b / _b.to_f * 100.0]
|
141
|
+
end
|
142
|
+
io_pop.should == "A: %5.1f%% > B: 100.0%%\n" % [(a + 1) / _a.to_f * 100.0]
|
143
|
+
end
|
144
|
+
io_pop.should == "A: 100.0%\nA: 100.0%\n\n"
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should not overlap outer progress if inner exceeds [#{_a}, #{_b}]" do
|
148
|
+
_a.times_with_progress('A') do |a|
|
149
|
+
io_pop.should == "A: #{a == 0 ? '......' : '%5.1f%%'}\n" % [a / _a.to_f * 100.0]
|
150
|
+
Progress.start('B', _b) do
|
151
|
+
(_b * 2).times do |b|
|
152
|
+
io_pop.should == "A: #{a == 0 && b == 0 ? '......' : '%5.1f%%'} > B: #{b == 0 ? '......' : '%5.1f%%'}\n" % [(a + [b / _b.to_f, 1].min) / _a.to_f * 100.0, b / _b.to_f * 100.0]
|
153
|
+
Progress.step
|
154
|
+
end
|
155
|
+
end
|
156
|
+
io_pop.should == "A: %5.1f%% > B: 200.0%%\n" % [(a + 1) / _a.to_f * 100.0]
|
157
|
+
end
|
158
|
+
io_pop.should == "A: 100.0%\nA: 100.0%\n\n"
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should allow step with block to validly count custom progresses [#{_a}, #{_b}]" do
|
162
|
+
a_step = 99
|
163
|
+
Progress.start('A', _a * 100) do
|
164
|
+
io_pop.should == "A: ......\n"
|
165
|
+
_a.times do |a|
|
166
|
+
Progress.step(a_step) do
|
167
|
+
_b.times_with_progress('B') do |b|
|
168
|
+
io_pop.should == "A: #{a == 0 && b == 0 ? '......' : '%5.1f%%'} > B: #{b == 0 ? '......' : '%5.1f%%'}\n" % [(a * a_step + b / _b.to_f * a_step) / (_a * 100).to_f * 100.0, b / _b.to_f * 100.0]
|
169
|
+
end
|
170
|
+
io_pop.should == "A: %5.1f%% > B: 100.0%\n" % [(a + 1) * a_step.to_f / (100.0 * _a.to_f) * 100.0]
|
171
|
+
end
|
172
|
+
io_pop.should == "A: %5.1f%%\n" % [(a + 1) * a_step.to_f / (100.0 * _a.to_f) * 100.0]
|
173
|
+
end
|
174
|
+
Progress.step _a
|
175
|
+
end
|
176
|
+
io_pop.should == "A: 100.0%\nA: 100.0%\n\n"
|
40
177
|
end
|
41
178
|
end
|
42
|
-
verify_output_after_stop
|
43
179
|
end
|
44
180
|
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
45
187
|
describe Enumerable do
|
46
188
|
before :each do
|
47
189
|
@a = (0...1000).to_a
|
@@ -62,6 +204,13 @@ describe Progress do
|
|
62
204
|
end
|
63
205
|
verify_output_after_stop
|
64
206
|
end
|
207
|
+
|
208
|
+
it "should show valid output for each_with_progress without title" do
|
209
|
+
@a.each_with_progress do |n|
|
210
|
+
verify_output_before_step(n)
|
211
|
+
end
|
212
|
+
verify_output_after_stop
|
213
|
+
end
|
65
214
|
end
|
66
215
|
|
67
216
|
describe 'with each_with_index_and_progress' do
|
@@ -80,6 +229,13 @@ describe Progress do
|
|
80
229
|
end
|
81
230
|
verify_output_after_stop
|
82
231
|
end
|
232
|
+
|
233
|
+
it "should show valid output for each_with_progress without title" do
|
234
|
+
@a.each_with_index_and_progress do |n, i|
|
235
|
+
verify_output_before_step(n)
|
236
|
+
end
|
237
|
+
verify_output_after_stop
|
238
|
+
end
|
83
239
|
end
|
84
240
|
|
85
241
|
describe 'with with_progress' do
|
@@ -132,116 +288,13 @@ describe Progress do
|
|
132
288
|
end
|
133
289
|
verify_output_after_stop
|
134
290
|
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
it "should pipe result from block" do
|
139
|
-
Progress.start('Test') do
|
140
|
-
'qwerty'
|
141
|
-
end.should == 'qwerty'
|
142
|
-
end
|
143
|
-
|
144
|
-
it "should not raise errors on extra Progress.stop" do
|
145
|
-
proc{
|
146
|
-
10.times_with_progress('10') do
|
147
|
-
Progress.start 'simple' do
|
148
|
-
Progress.start 'procedural'
|
149
|
-
Progress.stop
|
150
|
-
Progress.stop
|
151
|
-
end
|
152
|
-
Progress.stop
|
153
|
-
end
|
154
|
-
Progress.stop
|
155
|
-
}.should_not raise_error
|
156
|
-
end
|
157
|
-
|
158
|
-
it "should pipe result from nested block" do
|
159
|
-
[1, 2, 3].with_progress('a').map do |a|
|
160
|
-
[1, 2, 3].with_progress('b').map do |b|
|
161
|
-
a * b
|
162
|
-
end
|
163
|
-
end.should == [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
|
164
|
-
end
|
165
291
|
|
166
|
-
|
167
|
-
|
168
|
-
catch(:lalala) do
|
169
|
-
2.times_with_progress('A') do |a|
|
170
|
-
io_pop.should == "A: ......\n"
|
171
|
-
2.times_with_progress('B') do |b|
|
172
|
-
io_pop.should == "A: ...... > B: ......\n"
|
173
|
-
throw(:lalala)
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
io_pop.should == "A: ......\n\n"
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
[[2, 2000], [20, 200], [200, 20], [2000, 2]].each do |_a, _b|
|
182
|
-
it "should allow enclosed progress [#{_a}, #{_b}]" do
|
183
|
-
_a.times_with_progress('A') do |a|
|
184
|
-
io_pop.should == "A: #{a == 0 ? '......' : '%5.1f%%'}\n" % [a / _a.to_f * 100.0]
|
185
|
-
_b.times_with_progress('B') do |b|
|
186
|
-
io_pop.should == "A: #{a == 0 && b == 0 ? '......' : '%5.1f%%'} > B: #{b == 0 ? '......' : '%5.1f%%'}\n" % [(a + b / _b.to_f) / _a.to_f * 100.0, b / _b.to_f * 100.0]
|
187
|
-
end
|
188
|
-
io_pop.should == "A: %5.1f%% > B: 100.0%%\n" % [(a + 1) / _a.to_f * 100.0]
|
189
|
-
end
|
190
|
-
io_pop.should == "A: 100.0%\nA: 100.0%\n\n"
|
191
|
-
end
|
192
|
-
|
193
|
-
it "should not overlap outer progress if inner exceeds [#{_a}, #{_b}]" do
|
194
|
-
_a.times_with_progress('A') do |a|
|
195
|
-
io_pop.should == "A: #{a == 0 ? '......' : '%5.1f%%'}\n" % [a / _a.to_f * 100.0]
|
196
|
-
Progress.start('B', _b) do
|
197
|
-
(_b * 2).times do |b|
|
198
|
-
io_pop.should == "A: #{a == 0 && b == 0 ? '......' : '%5.1f%%'} > B: #{b == 0 ? '......' : '%5.1f%%'}\n" % [(a + [b / _b.to_f, 1].min) / _a.to_f * 100.0, b / _b.to_f * 100.0]
|
199
|
-
Progress.step
|
200
|
-
end
|
201
|
-
end
|
202
|
-
io_pop.should == "A: %5.1f%% > B: 200.0%%\n" % [(a + 1) / _a.to_f * 100.0]
|
203
|
-
end
|
204
|
-
io_pop.should == "A: 100.0%\nA: 100.0%\n\n"
|
205
|
-
end
|
206
|
-
|
207
|
-
it "should allow step with block to validly count custom progresses [#{_a}, #{_b}]" do
|
208
|
-
a_step = 99
|
209
|
-
Progress.start('A', _a * 100) do
|
210
|
-
io_pop.should == "A: ......\n"
|
211
|
-
_a.times do |a|
|
212
|
-
Progress.step(a_step) do
|
213
|
-
_b.times_with_progress('B') do |b|
|
214
|
-
io_pop.should == "A: #{a == 0 && b == 0 ? '......' : '%5.1f%%'} > B: #{b == 0 ? '......' : '%5.1f%%'}\n" % [(a * a_step + b / _b.to_f * a_step) / (_a * 100).to_f * 100.0, b / _b.to_f * 100.0]
|
215
|
-
end
|
216
|
-
io_pop.should == "A: %5.1f%% > B: 100.0%\n" % [(a + 1) * a_step.to_f / (100.0 * _a.to_f) * 100.0]
|
217
|
-
end
|
218
|
-
io_pop.should == "A: %5.1f%%\n" % [(a + 1) * a_step.to_f / (100.0 * _a.to_f) * 100.0]
|
219
|
-
end
|
220
|
-
Progress.step _a
|
221
|
-
end
|
222
|
-
io_pop.should == "A: 100.0%\nA: 100.0%\n\n"
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
describe "using Progress instead of Progress.start" do
|
227
|
-
it "should show valid output for procedural version" do
|
228
|
-
Progress('Test', 1000)
|
229
|
-
1000.times do |i|
|
230
|
-
verify_output_before_step(i)
|
231
|
-
Progress.step
|
232
|
-
end
|
233
|
-
Progress.stop
|
234
|
-
verify_output_after_stop
|
235
|
-
end
|
236
|
-
|
237
|
-
it "should show valid output for block version" do
|
238
|
-
Progress('Test', 1000) do
|
239
|
-
1000.times do |i|
|
292
|
+
it "should show valid output for each_with_progress without title" do
|
293
|
+
1000.times_with_progress do |i|
|
240
294
|
verify_output_before_step(i)
|
241
|
-
Progress.step
|
242
295
|
end
|
296
|
+
verify_output_after_stop
|
243
297
|
end
|
244
|
-
verify_output_after_stop
|
245
298
|
end
|
246
299
|
end
|
247
300
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: progress
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Boba Fat
|
@@ -34,8 +34,10 @@ files:
|
|
34
34
|
- Rakefile
|
35
35
|
- VERSION
|
36
36
|
- lib/progress.rb
|
37
|
+
- lib/progress/active_record.rb
|
37
38
|
- lib/progress/enumerable.rb
|
38
39
|
- lib/progress/integer.rb
|
40
|
+
- lib/progress/kernel.rb
|
39
41
|
- lib/progress/with_progress.rb
|
40
42
|
- progress.gemspec
|
41
43
|
- spec/progress_spec.rb
|