Skip to content

Commit a568206

Browse files
committed
Refactor: Update Projection To Keep Track Of Past Projections
Why This Change Is Necessary ======================================================================== Previous projections are valuable information for some projectors and so each projector needs to have the capability of tracking it if it so chooses. What These Changes Do To Address the Issue ======================================================================== Track the projection in a new variable. Side Effects Caused By This Change ======================================================================== None expected.
1 parent dab70bb commit a568206

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

lib/ruby-progressbar/calculators/smoothed_average.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,33 @@ class SmoothedAverage
44
DEFAULT_STRENGTH = 0.1
55

66
attr_accessor :strength
7+
attr_reader :projection
78

89
def initialize(options = {})
9-
self.strength = options[:strength] || DEFAULT_STRENGTH
10+
self.projection = 0.0
11+
self.strength = options[:strength] || DEFAULT_STRENGTH
1012
end
1113

12-
def calculate(current_projection, new_value)
13-
self.class.calculate(
14-
current_projection,
15-
new_value,
16-
strength
17-
)
14+
def start(_options = {})
15+
self.projection = 0.0
16+
end
17+
18+
def calculate(new_value)
19+
self.projection = \
20+
self.class.calculate(
21+
@projection,
22+
new_value,
23+
strength
24+
)
1825
end
1926

2027
def self.calculate(current_projection, new_value, rate)
2128
(new_value * (1.0 - rate)) + (current_projection * rate)
2229
end
30+
31+
protected
32+
33+
attr_writer :projection
2334
end
2435
end
2536
end

lib/ruby-progressbar/progress.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ class Progress
99
:progress
1010

1111
attr_accessor :starting_position,
12-
:running_average,
1312
:running_average_calculator
1413

1514
def initialize(options = {})
1615
self.total = options.fetch(:total, DEFAULT_TOTAL)
1716
self.running_average_calculator = options[:projector]
1817

19-
start :at => DEFAULT_BEGINNING_POSITION
18+
start(:at => DEFAULT_BEGINNING_POSITION)
2019
end
2120

2221
def start(options = {})
23-
self.running_average = 0
24-
self.progress = \
22+
running_average_calculator.start
23+
self.progress = \
2524
self.starting_position = options[:at] || progress
2625
end
2726

@@ -54,7 +53,7 @@ def decrement
5453
end
5554

5655
def reset
57-
start :at => starting_position
56+
start(:at => starting_position)
5857
end
5958

6059
def progress=(new_progress)
@@ -64,9 +63,11 @@ def progress=(new_progress)
6463
end
6564

6665
@progress = new_progress
66+
running_average_calculator.calculate(absolute)
67+
end
6768

68-
self.running_average = running_average_calculator.calculate(running_average,
69-
absolute)
69+
def running_average
70+
running_average_calculator.projection
7071
end
7172

7273
def total=(new_total)

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class ProgressBar
55
module Calculators
66
describe SmoothedAverage do
7-
it 'can properly calculate a projector' do
7+
it 'can properly calculate a projection' do
88
first_projection = SmoothedAverage.calculate(4.5, 12, 0.1)
99
expect(first_projection).to be_within(0.001).of 11.25
1010

@@ -15,6 +15,20 @@ module Calculators
1515
expect(third_projection).to be_within(0.001).of 65.662
1616
end
1717

18+
describe '#start' do
19+
it 'resets the projection' do
20+
projector = SmoothedAverage.new
21+
projector.start
22+
projector.calculate(10)
23+
24+
expect(projector.projection).not_to be_zero
25+
26+
projector.start
27+
28+
expect(projector.projection).to be 0.0
29+
end
30+
end
31+
1832
describe '#strength' do
1933
it 'allows the default strength to be overridden' do
2034
projector = SmoothedAverage.new(:strength => 0.3)

spec/lib/ruby-progressbar/progress_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class ProgressBar
123123
projector = Calculators::SmoothedAverage.new
124124
progress = Progress.new(:total => 50, :projector => projector)
125125

126-
progress.running_average = 10
126+
projector.__send__(:projection=, 10)
127127
progress.start :at => 0
128128

129129
expect(progress.running_average).to be_zero
@@ -137,7 +137,7 @@ class ProgressBar
137137
projector = Calculators::SmoothedAverage.new
138138
progress = Progress.new(:total => 50, :projector => projector)
139139

140-
progress.running_average = 10
140+
projector.__send__(:projection=, 10)
141141
progress.start :at => 0
142142

143143
expect(progress.running_average).to be_zero

0 commit comments

Comments
 (0)