Skip to content

Commit dab70bb

Browse files
committed
Remove: smoothing Option
Why This Change Is Necessary ======================================================================== The plan is to allow different types of projectors to be used by the gem at some point and not all of those projectors are going to need the option to smooth, so instead we will change the configuration to allow for each projector to have their own configuration. What These Changes Do To Address the Issue ======================================================================== We add a deprecation warning for anyone using `smoothing` as an option and also add the new approach. The new approach will be instead of passing this: ```ruby ProgressBar.create(smoothing: 0.1) ``` You would pass: ```ruby ProgressBar.create(projector: { smoothing: 0.1 }) ``` Side Effects Caused By This Change ======================================================================== Additional warnings for anyone using `smoothing`.
1 parent 3ba0120 commit dab70bb

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

lib/ruby-progressbar/base.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ class ProgressBar
1717
class Base
1818
extend Forwardable
1919

20+
# rubocop:disable Layout/HeredocIndentation
21+
SMOOTHING_DEPRECATION_WARNING = <<-HEREDOC.tr("\n", ' ')
22+
WARNING: Passing the 'smoothing' option is deprecated and will be removed
23+
in version 2.0. Please pass { projector: { type: 'smoothing', strength: 0.x }}.
24+
For more information on why this change is happening, visit
25+
https://github.com/jfelchner/ruby-progressbar/wiki/Upgrading
26+
HEREDOC
27+
28+
RUNNING_AVERAGE_RATE_DEPRECATION_WARNING = <<-HEREDOC.tr("\n", ' ')
29+
WARNING: Passing the 'running_average_rate' option is deprecated and will be removed
30+
in version 2.0. Please pass { projector: { type: 'smoothing', strength: 0.x }}.
31+
For more information on why this change is happening, visit
32+
https://github.com/jfelchner/ruby-progressbar/wiki/Upgrading
33+
HEREDOC
34+
# rubocop:enable Layout/HeredocIndentation
35+
2036
def_delegators :output,
2137
:clear,
2238
:log,
@@ -32,7 +48,20 @@ def initialize(options = {}) # rubocop:disable Metrics/AbcSize
3248
self.finished = false
3349

3450
self.timer = Timer.new(options)
35-
self.projector = Calculators::SmoothedAverage.new(:strength => options[:smoothing])
51+
projector_opts = if options[:projector]
52+
options[:projector]
53+
elsif options[:smoothing]
54+
warn SMOOTHING_DEPRECATION_WARNING
55+
56+
{ :strength => options[:smoothing] }
57+
elsif options[:running_average_rate]
58+
warn RUNNING_AVERAGE_RATE_DEPRECATION_WARNING
59+
60+
{ :strength => options[:smoothing] }
61+
else
62+
{}
63+
end
64+
self.projector = Calculators::SmoothedAverage.new(projector_opts)
3665
self.progressable = Progress.new(options.merge(:projector => projector))
3766

3867
options = options.merge(:progress => progressable,

spec/lib/ruby-progressbar/base_spec.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class ProgressBar
232232
Timecop.freeze(::Time.utc(2020, 1, 1, 0, 0, 0))
233233

234234
progressbar = ProgressBar::Base.new(:output => output,
235-
:smoothing => 0.0,
235+
:projector => { :strength => 0.0 },
236236
:total => 100,
237237
:format => '%l')
238238

@@ -251,7 +251,7 @@ class ProgressBar
251251
Timecop.freeze(::Time.utc(2020, 1, 1, 0, 0, 0))
252252

253253
progressbar = ProgressBar::Base.new(:output => output,
254-
:smoothing => 0.0,
254+
:projector => { :strength => 0.0 },
255255
:total => 100,
256256
:format => '%l')
257257

@@ -275,6 +275,32 @@ class ProgressBar
275275
not_to raise_error
276276
end
277277

278+
# rubocop:disable RSpec/AnyInstance
279+
it 'displays a warning if the user passes the deprecated "smoothing" option but ' \
280+
'still processes it' do
281+
expect_any_instance_of(ProgressBar::Base).
282+
to receive(:warn).
283+
with(include("WARNING: Passing the 'smoothing' option is deprecated"))
284+
285+
Timecop.freeze(::Time.utc(2020, 1, 1, 0, 0, 0))
286+
287+
progressbar = ProgressBar::Base.new(:output => output,
288+
:smoothing => 0.0,
289+
:total => 100,
290+
:format => '%l')
291+
292+
progressbar.progress += 50
293+
294+
Timecop.freeze(::Time.utc(2020, 1, 1, 0, 30, 0))
295+
296+
progressbar.finish
297+
298+
Timecop.return
299+
300+
expect(progressbar.to_s).to eql '00:30:00'
301+
end
302+
# rubocop:enable RSpec/AnyInstance
303+
278304
it 'appends proper ending to string for non-TTY devices' do
279305
progressbar = ProgressBar::Base.new(:output => non_tty_output)
280306

spec/lib/ruby-progressbar/format/formatter_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ module Format
138138
end
139139

140140
it 'is the estimated time remaining when called after the bar is started' do
141-
progressbar = ProgressBar::Base.new(:smoothing => 0.0)
141+
progressbar = ProgressBar::Base.new(:projector => { :strength => 0.0 })
142142

143143
Timecop.freeze(to_the_past) do
144144
progressbar.start
@@ -150,7 +150,7 @@ module Format
150150

151151
it 'is "??:??:??" when it could take 100 hours or longer to finish' do
152152
progressbar = ProgressBar::Base.new(:total => 100,
153-
:smoothing => 0.0)
153+
:projector => { :strength => 0.0 })
154154

155155
Timecop.freeze(one_hundred_hours_ago) do
156156
progressbar.start
@@ -207,7 +207,7 @@ module Format
207207
progressbar = nil
208208

209209
Timecop.freeze(::Time.utc(2020, 1, 1, 0, 0, 0)) do
210-
progressbar = ProgressBar::Base.new(:smoothing => 0.0)
210+
progressbar = ProgressBar::Base.new(:projector => { :strength => 0.0 })
211211

212212
progressbar.start
213213
progressbar.progress = 50
@@ -262,7 +262,7 @@ module Format
262262
end
263263

264264
it 'is the estimated time remaining when called after the bar is started' do
265-
progressbar = ProgressBar::Base.new(:smoothing => 0.0)
265+
progressbar = ProgressBar::Base.new(:projector => { :strength => 0.0 })
266266

267267
Timecop.freeze(to_the_past) do
268268
progressbar.start
@@ -288,7 +288,7 @@ module Format
288288

289289
it 'is "> 4 Days" when it could take 100 hours or longer to finish' do
290290
progressbar = ProgressBar::Base.new(:total => 100,
291-
:smoothing => 0.0)
291+
:projector => { :strength => 0.0 })
292292

293293
Timecop.freeze(one_hundred_hours_ago) do
294294
progressbar.start
@@ -328,7 +328,7 @@ module Format
328328
end
329329

330330
it 'is the estimated time remaining when called after the bar is started' do
331-
progressbar = ProgressBar::Base.new(:smoothing => 0.0)
331+
progressbar = ProgressBar::Base.new(:projector => { :strength => 0.0 })
332332

333333
Timecop.freeze(to_the_past) do
334334
progressbar.start
@@ -340,7 +340,7 @@ module Format
340340

341341
it 'is the exact estimated time when it could take 100 hours or longer to finish' do
342342
progressbar = ProgressBar::Base.new(:total => 100,
343-
:smoothing => 0.0)
343+
:projector => { :strength => 0.0 })
344344

345345
Timecop.freeze(one_hundred_hours_ago) do
346346
progressbar.start

0 commit comments

Comments
 (0)