Skip to content

Commit 7ab80e3

Browse files
committed
Refactor: Projector To Be Able To Keep Track Of Progress
Why This Change Is Necessary ======================================================================== Previously we were passing new values into `calculate` but were not saving them. In order to be able to be more useful in more situations, we need to have the projector be called each time progress is updated and be able to save that data if necessary for future projections. This particular calculator only needs two values (the starting point and the current point) but other projectors may need more (for example a projector that only cares about the last n minutes of items or one that only cares about the last x items). What These Changes Do To Address the Issue ======================================================================== Because the projector is now tied into progress events, all we have to do is implement those methods and save the data as necessary. We introduce a concept of "samples" into the projector. Each projector will be responsible for holding as many samples of progress data as is necessary for it to do its job properly. Side Effects Caused By This Change ======================================================================== None expected.
1 parent e6b09ad commit 7ab80e3

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

lib/ruby-progressbar/calculators/smoothed_average.rb

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
11
class ProgressBar
22
module Calculators
33
class SmoothedAverage
4-
DEFAULT_STRENGTH = 0.1
4+
DEFAULT_STRENGTH = 0.1
5+
DEFAULT_BEGINNING_POSITION = 0
56

6-
attr_accessor :strength
7+
attr_accessor :samples,
8+
:strength
79
attr_reader :projection
810

911
def initialize(options = {})
12+
self.samples = []
1013
self.projection = 0.0
1114
self.strength = options[:strength] || DEFAULT_STRENGTH
15+
16+
start(:at => DEFAULT_BEGINNING_POSITION)
1217
end
1318

14-
def start(_options = {})
19+
def start(options = {})
1520
self.projection = 0.0
21+
self.progress = samples[0] = (options[:at] || progress)
22+
end
23+
24+
def decrement
25+
self.progress -= 1
26+
end
27+
28+
def increment
29+
self.progress += 1
30+
end
31+
32+
def progress
33+
samples[1]
34+
end
35+
36+
def progress=(new_progress)
37+
samples[1] = new_progress
38+
calculate(absolute)
1639
end
1740

18-
def decrement; end
19-
def increment; end
20-
def progress=(_new_progress); end
2141
def total=(_new_total); end
2242

2343
def reset
24-
start
44+
start(:at => samples[0])
2545
end
2646

2747
def calculate(new_value)
@@ -44,6 +64,12 @@ def self.calculate(current_projection, new_value, rate)
4464
protected
4565

4666
attr_writer :projection
67+
68+
private
69+
70+
def absolute
71+
samples[1] - samples[0]
72+
end
4773
end
4874
end
4975
end

lib/ruby-progressbar/progress.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def progress=(new_progress)
6262
end
6363

6464
@progress = new_progress
65-
running_average_calculator.calculate(absolute)
6665
end
6766

6867
def running_average

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ module Calculators
4141

4242
expect(projector.projection).to be 0.0
4343
end
44+
45+
it 'resets based on the starting position' do
46+
projector = SmoothedAverage.new(:strength => 0.1)
47+
projector.start(:at => 10)
48+
projector.progress = 20
49+
50+
expect(projector.projection).not_to be_zero
51+
52+
projector.reset
53+
projector.progress = 20
54+
55+
expect(projector.projection).to be 9.0
56+
end
4457
end
4558

4659
describe '#strength' do

spec/lib/ruby-progressbar/progress_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class ProgressBar
129129

130130
expect(progress.running_average).to be_zero
131131

132+
projector.progress += 40
132133
progress.progress += 40
133134

134135
expect(progress.running_average).to eql 36.0

0 commit comments

Comments
 (0)