Skip to content

Commit 616a7b3

Browse files
committed
Refactor: The Progress No Longer Needs The Projector
Why This Change Is Necessary ======================================================================== We've removed all usages of the projector from the `Progress` object and therefore it is no longer needed to do its job. What These Changes Do To Address the Issue ======================================================================== Remove all usages of the projector from `Progress` and update test setup to reflect that. Side Effects Caused By This Change ======================================================================== None expected.
1 parent 993bcf3 commit 616a7b3

File tree

5 files changed

+90
-119
lines changed

5 files changed

+90
-119
lines changed

lib/ruby-progressbar/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def initialize(options = {}) # rubocop:disable Metrics/AbcSize
6262
{}
6363
end
6464
self.projector = Calculators::SmoothedAverage.new(projector_opts)
65-
self.progressable = Progress.new(options.merge(:projector => projector))
65+
self.progressable = Progress.new(options)
6666

6767
options = options.merge(:progress => progressable,
6868
:projector => projector,

lib/ruby-progressbar/progress.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ class Progress
77

88
attr_reader :total,
99
:progress
10-
11-
attr_accessor :starting_position,
12-
:running_average_calculator
10+
attr_accessor :starting_position
1311

1412
def initialize(options = {})
15-
self.total = options.fetch(:total, DEFAULT_TOTAL)
16-
self.running_average_calculator = options[:projector]
13+
self.total = options.fetch(:total, DEFAULT_TOTAL)
1714

1815
start(:at => DEFAULT_BEGINNING_POSITION)
1916
end
@@ -64,10 +61,6 @@ def progress=(new_progress)
6461
@progress = new_progress
6562
end
6663

67-
def running_average
68-
running_average_calculator.projection
69-
end
70-
7164
def total=(new_total)
7265
unless progress.nil? || new_total.nil? || new_total >= progress
7366
fail ProgressBar::InvalidProgressError,

spec/lib/ruby-progressbar/calculators/smoothed_average_spec.rb

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,54 @@
44
class ProgressBar
55
module Calculators
66
describe SmoothedAverage do
7-
it 'can properly calculate a projection' do
8-
first_projection = SmoothedAverage.calculate(4.5, 12, 0.1)
9-
expect(first_projection).to be_within(0.001).of 11.25
7+
describe '.calculate' do
8+
it 'can properly calculate a projection' do
9+
first_projection = SmoothedAverage.calculate(4.5, 12, 0.1)
10+
expect(first_projection).to be_within(0.001).of 11.25
1011

11-
second_projection = SmoothedAverage.calculate(8.2, 51, 0.7)
12-
expect(second_projection).to be_within(0.001).of 21.04
12+
second_projection = SmoothedAverage.calculate(8.2, 51, 0.7)
13+
expect(second_projection).to be_within(0.001).of 21.04
1314

14-
third_projection = SmoothedAverage.calculate(41.8, 100, 0.59)
15-
expect(third_projection).to be_within(0.001).of 65.662
15+
third_projection = SmoothedAverage.calculate(41.8, 100, 0.59)
16+
expect(third_projection).to be_within(0.001).of 65.662
17+
end
18+
end
19+
20+
describe '#projection' do
21+
it 'can properly calculate a running average' do
22+
projector = SmoothedAverage.new(:strength => 0.1)
23+
projector.start
24+
projector.progress = 5
25+
projector.progress = 12
26+
27+
expect(projector.projection).to be_within(0.001).of 11.25
28+
end
29+
30+
it 'knows the running average even when progress has been made' do
31+
projector = SmoothedAverage.new(:total => 50)
32+
33+
projector.instance_variable_set(:@projection, 10)
34+
projector.start :at => 0
35+
36+
expect(projector.projection).to be_zero
37+
38+
projector.progress += 40
39+
40+
expect(projector.projection).to be 36.0
41+
end
42+
43+
it 'knows the running average is reset even after progress is started' do
44+
projector = SmoothedAverage.new(:total => 50)
45+
46+
projector.instance_variable_set(:@projection, 10)
47+
projector.start :at => 0
48+
49+
expect(projector.projection).to be_zero
50+
51+
projector.start :at => 40
52+
53+
expect(projector.projection).to be 0.0
54+
end
1655
end
1756

1857
describe '#start' do

spec/lib/ruby-progressbar/components/time_spec.rb

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Components
88
it 'displays unknown elapsed time when the timer has not been started' do
99
timer = Timer.new
1010
projector = Calculators::SmoothedAverage.new
11-
progress = Progress.new(:projector => projector)
11+
progress = Progress.new
1212
time = Time.new(:timer => timer,
1313
:progress => progress,
1414
:projector => projector)
@@ -19,7 +19,7 @@ module Components
1919
it 'displays elapsed time when the timer has just been started' do
2020
timer = Timer.new
2121
projector = Calculators::SmoothedAverage.new
22-
progress = Progress.new(:projector => projector)
22+
progress = Progress.new
2323
time = Time.new(:timer => timer,
2424
:progress => progress,
2525
:projector => projector)
@@ -33,7 +33,7 @@ module Components
3333
it 'displays elapsed time if it was previously started' do
3434
timer = Timer.new
3535
projector = Calculators::SmoothedAverage.new
36-
progress = Progress.new(:projector => projector)
36+
progress = Progress.new
3737
time = Time.new(:timer => timer,
3838
:progress => progress,
3939
:projector => projector)
@@ -51,7 +51,7 @@ module Components
5151
it 'displays elapsed time frozen to a specific time if it was previously stopped' do
5252
timer = Timer.new
5353
projector = Calculators::SmoothedAverage.new
54-
progress = Progress.new(:projector => projector)
54+
progress = Progress.new
5555
time = Time.new(:timer => timer,
5656
:progress => progress,
5757
:projector => projector)
@@ -74,7 +74,7 @@ module Components
7474
it 'displays unknown elapsed time after reset has been called' do
7575
timer = Timer.new
7676
projector = Calculators::SmoothedAverage.new
77-
progress = Progress.new(:projector => projector)
77+
progress = Progress.new
7878
time = Time.new(:timer => timer,
7979
:progress => progress,
8080
:projector => projector)
@@ -96,8 +96,7 @@ module Components
9696
it 'displays estimated time if it is known' do
9797
timer = Timer.new
9898
projector = Calculators::SmoothedAverage.new(:strength => 0.0)
99-
progress = Progress.new(:projector => projector,
100-
:total => 100)
99+
progress = Progress.new(:total => 100)
101100
time = Time.new(:timer => timer,
102101
:progress => progress,
103102
:projector => projector)
@@ -120,7 +119,7 @@ module Components
120119
'but no progress has been made' do
121120
timer = Timer.new
122121
projector = Calculators::SmoothedAverage.new
123-
progress = Progress.new(:projector => projector, :total => 100)
122+
progress = Progress.new(:total => 100)
124123
time = Time.new(:timer => timer,
125124
:progress => progress,
126125
:projector => projector)
@@ -135,7 +134,7 @@ module Components
135134
'is reset' do
136135
timer = Timer.new
137136
projector = Calculators::SmoothedAverage.new
138-
progress = Progress.new(:projector => projector, :total => 100)
137+
progress = Progress.new(:total => 100)
139138
time = Time.new(:timer => timer,
140139
:progress => progress,
141140
:projector => projector)
@@ -160,7 +159,7 @@ module Components
160159
'is reset' do
161160
timer = Timer.new
162161
projector = Calculators::SmoothedAverage.new
163-
progress = Progress.new(:projector => projector, :total => 100)
162+
progress = Progress.new(:total => 100)
164163
time = Time.new(:timer => timer,
165164
:progress => progress,
166165
:projector => projector)
@@ -185,8 +184,7 @@ module Components
185184
'and the out of bounds format is set to "unknown"' do
186185
timer = Timer.new
187186
projector = Calculators::SmoothedAverage.new(:strength => 0.0)
188-
progress = Progress.new(:projector => projector,
189-
:total => 100)
187+
progress = Progress.new(:total => 100)
190188
time = Time.new(:out_of_bounds_time_format => :unknown,
191189
:timer => timer,
192190
:progress => progress,
@@ -210,8 +208,7 @@ module Components
210208
'is made' do
211209
timer = Timer.new
212210
projector = Calculators::SmoothedAverage.new(:strength => 0.5)
213-
progress = Progress.new(:projector => projector,
214-
:total => 100)
211+
progress = Progress.new(:total => 100)
215212
time = Time.new(:timer => timer,
216213
:progress => progress,
217214
:projector => projector)
@@ -237,8 +234,7 @@ module Components
237234
it 'displays estimated time if it is known' do
238235
timer = Timer.new
239236
projector = Calculators::SmoothedAverage.new(:strength => 0.0)
240-
progress = Progress.new(:projector => projector,
241-
:total => 100)
237+
progress = Progress.new(:total => 100)
242238
time = Time.new(:timer => timer,
243239
:progress => progress,
244240
:projector => projector)
@@ -261,8 +257,7 @@ module Components
261257
'and the out of bounds format is set to "friendly"' do
262258
timer = Timer.new
263259
projector = Calculators::SmoothedAverage.new(:strength => 0.0)
264-
progress = Progress.new(:projector => projector,
265-
:total => 100)
260+
progress = Progress.new(:total => 100)
266261
time = Time.new(:out_of_bounds_time_format => :friendly,
267262
:timer => timer,
268263
:progress => progress,
@@ -287,8 +282,7 @@ module Components
287282
it 'displays estimated time if it is known' do
288283
timer = Timer.new
289284
projector = Calculators::SmoothedAverage.new(:strength => 0.0)
290-
progress = Progress.new(:projector => projector,
291-
:total => 100)
285+
progress = Progress.new(:total => 100)
292286
time = Time.new(:timer => timer,
293287
:progress => progress,
294288
:projector => projector)
@@ -311,8 +305,7 @@ module Components
311305
'out of bounds format is unset' do
312306
timer = Timer.new
313307
projector = Calculators::SmoothedAverage.new(:strength => 0.0)
314-
progress = Progress.new(:projector => projector,
315-
:total => 100)
308+
progress = Progress.new(:total => 100)
316309
time = Time.new(:out_of_bounds_time_format => nil,
317310
:timer => timer,
318311
:progress => progress,
@@ -338,8 +331,7 @@ module Components
338331
'it is incremented' do
339332
timer = Timer.new
340333
projector = Calculators::SmoothedAverage.new
341-
progress = Progress.new(:projector => projector,
342-
:total => 100)
334+
progress = Progress.new(:total => 100)
343335
time = Time.new(:timer => timer,
344336
:progress => progress,
345337
:projector => projector)
@@ -355,8 +347,7 @@ module Components
355347
it 'displays unsmoothed time remaining when progress has been made' do
356348
timer = Timer.new
357349
projector = Calculators::SmoothedAverage.new(:strength => 0.0)
358-
progress = Progress.new(:projector => projector,
359-
:total => 100)
350+
progress = Progress.new(:total => 100)
360351
time = Time.new(:timer => timer,
361352
:progress => progress,
362353
:projector => projector)
@@ -379,8 +370,7 @@ module Components
379370
'bar is decremented' do
380371
timer = Timer.new
381372
projector = Calculators::SmoothedAverage.new(:strength => 0.0)
382-
progress = Progress.new(:projector => projector,
383-
:total => 100)
373+
progress = Progress.new(:total => 100)
384374
time = Time.new(:timer => timer,
385375
:progress => progress,
386376
:projector => projector)
@@ -408,8 +398,7 @@ module Components
408398
'account' do
409399
timer = Timer.new
410400
projector = Calculators::SmoothedAverage.new(:strength => 0.5)
411-
progress = Progress.new(:projector => projector,
412-
:total => 100)
401+
progress = Progress.new(:total => 100)
413402
time = Time.new(:timer => timer,
414403
:progress => progress,
415404
:projector => projector)
@@ -436,8 +425,7 @@ module Components
436425
it 'displays smoothed estimated time after progress has been made' do
437426
timer = Timer.new
438427
projector = Calculators::SmoothedAverage.new(:strength => 0.5)
439-
progress = Progress.new(:projector => projector,
440-
:total => 100)
428+
progress = Progress.new(:total => 100)
441429
time = Time.new(:timer => timer,
442430
:progress => progress,
443431
:projector => projector)
@@ -460,8 +448,7 @@ module Components
460448
'very short intervals' do
461449
timer = Timer.new
462450
projector = Calculators::SmoothedAverage.new(:strength => 0.1)
463-
progress = Progress.new(:projector => projector,
464-
:total => 10)
451+
progress = Progress.new(:total => 10)
465452
time = Time.new(:timer => timer,
466453
:progress => progress,
467454
:projector => projector)
@@ -506,7 +493,7 @@ module Components
506493
it 'displays the wall clock time as unknown when the timer has been reset' do
507494
timer = Timer.new
508495
projector = Calculators::SmoothedAverage.new
509-
progress = Progress.new(:projector => projector)
496+
progress = Progress.new
510497
time = Time.new(:timer => timer,
511498
:progress => progress,
512499
:projector => projector)
@@ -526,7 +513,7 @@ module Components
526513
it 'displays the wall clock time as unknown when the progress has not begun' do
527514
timer = Timer.new
528515
projector = Calculators::SmoothedAverage.new
529-
progress = Progress.new(:projector => projector)
516+
progress = Progress.new
530517
time = Time.new(:timer => timer,
531518
:progress => progress,
532519
:projector => projector)
@@ -544,7 +531,7 @@ module Components
544531
it 'displays the completed wall clock time if the progress is finished' do
545532
timer = Timer.new
546533
projector = Calculators::SmoothedAverage.new
547-
progress = Progress.new(:projector => projector)
534+
progress = Progress.new
548535
time = Time.new(:timer => timer,
549536
:progress => progress,
550537
:projector => projector)
@@ -567,7 +554,7 @@ module Components
567554
it 'displays the estimated wall clock time if the progress is ongoing' do
568555
timer = Timer.new
569556
projector = Calculators::SmoothedAverage.new(:strength => 0.0)
570-
progress = Progress.new(:projector => projector)
557+
progress = Progress.new
571558
time = Time.new(:timer => timer,
572559
:progress => progress,
573560
:projector => projector)

0 commit comments

Comments
 (0)