Skip to content

Commit

Permalink
Refactor: Pass Projector To The Time Component
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jfelchner committed Mar 4, 2023
1 parent 611b997 commit 0c956e6
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 87 deletions.
9 changes: 8 additions & 1 deletion lib/ruby-progressbar/calculators/smoothed_average.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def decrement; end
def increment; end
def progress=(_new_progress); end
def total=(_new_total); end
def reset; end

def reset
start
end

def calculate(new_value)
self.projection = \
Expand All @@ -30,6 +33,10 @@ def calculate(new_value)
)
end

def none?
projection.zero?
end

def self.calculate(current_projection, new_value, rate)
(new_value * (1.0 - rate)) + (current_projection * rate)
end
Expand Down
12 changes: 7 additions & 5 deletions lib/ruby-progressbar/components/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class Time
}.freeze

def initialize(options = {})
self.timer = options[:timer]
self.progress = options[:progress]
self.timer = options[:timer]
self.progress = options[:progress]
self.projector = options[:projector]
end

def estimated_with_label(out_of_bounds_time_format = nil)
Expand Down Expand Up @@ -57,7 +58,8 @@ def estimated_wall_clock
protected

attr_accessor :timer,
:progress
:progress,
:projector

private

Expand Down Expand Up @@ -90,9 +92,9 @@ def estimated_with_elapsed_fallback(out_of_bounds_time_format)
end

def estimated_seconds_remaining
return if progress.unknown? || progress.none? || timer.stopped? || timer.reset?
return if progress.unknown? || projector.none? || progress.none? || timer.stopped? || timer.reset?

(timer.elapsed_seconds * ((progress.total / progress.running_average) - 1)).round
(timer.elapsed_seconds * ((progress.total / projector.projection) - 1)).round
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-progressbar/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def percentage_completed
end

def none?
running_average.zero? || progress.zero?
progress.zero?
end

def unknown?
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/ruby-progressbar/calculators/smoothed_average_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ module Calculators
end
end

describe '#reset' do
it 'resets the projection' do
projector = SmoothedAverage.new
projector.start
projector.calculate(10)

expect(projector.projection).not_to be_zero

projector.reset

expect(projector.projection).to be 0.0
end
end

describe '#strength' do
it 'allows the default strength to be overridden' do
projector = SmoothedAverage.new(:strength => 0.3)
Expand Down
Loading

0 comments on commit 0c956e6

Please sign in to comment.