Skip to content

Commit e1e7f0c

Browse files
committed
Add: Ability To Allow Different Projectors To Be Used For Time Estimates
Why This Change Is Necessary ======================================================================== The current projector we've had in the code for over a decade now has served us well, but there has been a desire to have some different types of projectors (eg windowed by item count or by time). This was previously difficult to do but with this change we should be able to allow the user to swap different ones out fairly easily. What These Changes Do To Address the Issue ======================================================================== Add a new `Projector` class that manages all other projectors. Side Effects Caused By This Change ======================================================================== None expected.
1 parent 616a7b3 commit e1e7f0c

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

lib/ruby-progressbar/base.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'forwardable'
22

3-
require 'ruby-progressbar/calculators/smoothed_average'
43
require 'ruby-progressbar/components/bar'
54
require 'ruby-progressbar/components/percentage'
65
require 'ruby-progressbar/components/rate'
@@ -11,6 +10,7 @@
1110
require 'ruby-progressbar/outputs/non_tty'
1211
require 'ruby-progressbar/outputs/tty'
1312
require 'ruby-progressbar/progress'
13+
require 'ruby-progressbar/projector'
1414
require 'ruby-progressbar/timer'
1515

1616
class ProgressBar
@@ -43,12 +43,14 @@ class Base
4343
:total
4444

4545
def initialize(options = {}) # rubocop:disable Metrics/AbcSize
46+
options[:projector] ||= {}
47+
4648
self.autostart = options.fetch(:autostart, true)
4749
self.autofinish = options.fetch(:autofinish, true)
4850
self.finished = false
4951

5052
self.timer = Timer.new(options)
51-
projector_opts = if options[:projector]
53+
projector_opts = if options[:projector].any?
5254
options[:projector]
5355
elsif options[:smoothing]
5456
warn SMOOTHING_DEPRECATION_WARNING
@@ -61,7 +63,9 @@ def initialize(options = {}) # rubocop:disable Metrics/AbcSize
6163
else
6264
{}
6365
end
64-
self.projector = Calculators::SmoothedAverage.new(projector_opts)
66+
self.projector = Projector.
67+
from_type(options[:projector][:type]).
68+
new(projector_opts)
6569
self.progressable = Progress.new(options)
6670

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

lib/ruby-progressbar/projector.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'ruby-progressbar/calculators/smoothed_average'
2+
3+
class ProgressBar
4+
class Projector
5+
DEFAULT_PROJECTOR = ProgressBar::Calculators::SmoothedAverage
6+
NAME_TO_PROJECTOR_MAP = {
7+
'smoothed' => ProgressBar::Calculators::SmoothedAverage
8+
}.freeze
9+
10+
def self.from_type(name)
11+
NAME_TO_PROJECTOR_MAP.fetch(name, DEFAULT_PROJECTOR)
12+
end
13+
end
14+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'spec_helper'
2+
require 'ruby-progressbar/projector'
3+
4+
class ProgressBar
5+
describe Projector do
6+
describe '.from_type' do
7+
it 'has a default projector' do
8+
projector = Projector.from_type(nil)
9+
10+
expect(projector).to be ProgressBar::Calculators::SmoothedAverage
11+
end
12+
13+
it 'can return a specific projector' do
14+
projector = Projector.from_type('smoothed')
15+
16+
expect(projector).to be ProgressBar::Calculators::SmoothedAverage
17+
end
18+
end
19+
end
20+
end

0 commit comments

Comments
 (0)