Skip to content

Commit 0c956e6

Browse files
committed
Refactor: Pass Projector To The Time Component
Why This Change Is Necessary ======================================================================== The time component is the primary use case for the projector since it needs to represent the estimated amount of time left in the run. Currently the time component is getting the projection off the `Progress` object but at this point it is able to go directly to the `Projector`. What These Changes Do To Address the Issue ======================================================================== Pass the projector to the time component and use it instead of the progress. Side Effects Caused By This Change ======================================================================== None expected.
1 parent 611b997 commit 0c956e6

File tree

5 files changed

+227
-87
lines changed

5 files changed

+227
-87
lines changed

lib/ruby-progressbar/calculators/smoothed_average.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ def decrement; end
1919
def increment; end
2020
def progress=(_new_progress); end
2121
def total=(_new_total); end
22-
def reset; end
22+
23+
def reset
24+
start
25+
end
2326

2427
def calculate(new_value)
2528
self.projection = \
@@ -30,6 +33,10 @@ def calculate(new_value)
3033
)
3134
end
3235

36+
def none?
37+
projection.zero?
38+
end
39+
3340
def self.calculate(current_projection, new_value, rate)
3441
(new_value * (1.0 - rate)) + (current_projection * rate)
3542
end

lib/ruby-progressbar/components/time.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ class Time
1919
}.freeze
2020

2121
def initialize(options = {})
22-
self.timer = options[:timer]
23-
self.progress = options[:progress]
22+
self.timer = options[:timer]
23+
self.progress = options[:progress]
24+
self.projector = options[:projector]
2425
end
2526

2627
def estimated_with_label(out_of_bounds_time_format = nil)
@@ -57,7 +58,8 @@ def estimated_wall_clock
5758
protected
5859

5960
attr_accessor :timer,
60-
:progress
61+
:progress,
62+
:projector
6163

6264
private
6365

@@ -90,9 +92,9 @@ def estimated_with_elapsed_fallback(out_of_bounds_time_format)
9092
end
9193

9294
def estimated_seconds_remaining
93-
return if progress.unknown? || progress.none? || timer.stopped? || timer.reset?
95+
return if progress.unknown? || projector.none? || progress.none? || timer.stopped? || timer.reset?
9496

95-
(timer.elapsed_seconds * ((progress.total / progress.running_average) - 1)).round
97+
(timer.elapsed_seconds * ((progress.total / projector.projection) - 1)).round
9698
end
9799
end
98100
end

lib/ruby-progressbar/progress.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def percentage_completed
9292
end
9393

9494
def none?
95-
running_average.zero? || progress.zero?
95+
progress.zero?
9696
end
9797

9898
def unknown?

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ module Calculators
2929
end
3030
end
3131

32+
describe '#reset' do
33+
it 'resets the projection' do
34+
projector = SmoothedAverage.new
35+
projector.start
36+
projector.calculate(10)
37+
38+
expect(projector.projection).not_to be_zero
39+
40+
projector.reset
41+
42+
expect(projector.projection).to be 0.0
43+
end
44+
end
45+
3246
describe '#strength' do
3347
it 'allows the default strength to be overridden' do
3448
projector = SmoothedAverage.new(:strength => 0.3)

0 commit comments

Comments
 (0)