Skip to content

Commit 22a0210

Browse files
committed
Refactor: Remove out_of_bounds_time_format Instance Variable
1 parent 1274225 commit 22a0210

File tree

2 files changed

+17
-40
lines changed

2 files changed

+17
-40
lines changed

lib/ruby-progressbar/components/time.rb

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ class Time
1919
}.freeze
2020

2121
def initialize(options = {})
22-
self.out_of_bounds_time_format = options[:out_of_bounds_time_format]
23-
self.timer = options[:timer]
24-
self.progress = options[:progress]
22+
self.timer = options[:timer]
23+
self.progress = options[:progress]
2524
end
2625

27-
def estimated_with_label
28-
"#{ESTIMATED_LABEL}: #{estimated}"
26+
def estimated_with_label(out_of_bounds_time_format = nil)
27+
"#{ESTIMATED_LABEL}: #{estimated(out_of_bounds_time_format)}"
2928
end
3029

3130
def elapsed_with_label
@@ -35,21 +34,15 @@ def elapsed_with_label
3534
protected
3635

3736
def estimated_with_no_oob
38-
self.out_of_bounds_time_format = nil
39-
40-
estimated_with_elapsed_fallback
37+
estimated_with_elapsed_fallback(nil)
4138
end
4239

4340
def estimated_with_unknown_oob
44-
self.out_of_bounds_time_format = :unknown
45-
46-
estimated_with_elapsed_fallback
41+
estimated_with_elapsed_fallback(:unknown)
4742
end
4843

4944
def estimated_with_friendly_oob
50-
self.out_of_bounds_time_format = :friendly
51-
52-
estimated_with_elapsed_fallback
45+
estimated_with_elapsed_fallback(:friendly)
5346
end
5447

5548
def estimated_wall_clock
@@ -63,29 +56,20 @@ def estimated_wall_clock
6356
strftime(WALL_CLOCK_FORMAT)
6457
end
6558

66-
attr_reader :out_of_bounds_time_format
6759
attr_accessor :timer,
6860
:progress
6961

70-
def out_of_bounds_time_format=(format)
71-
unless OOB_TIME_FORMATS.include? format
72-
fail StandardError, "Invalid Out Of Bounds time format. Valid formats are #{OOB_TIME_FORMATS.inspect}"
73-
end
74-
75-
@out_of_bounds_time_format = format
76-
end
77-
7862
private
7963

80-
def estimated
64+
def estimated(out_of_bounds_time_format)
8165
memo_estimated_seconds_remaining = estimated_seconds_remaining
8266

8367
return OOB_UNKNOWN_TIME_TEXT unless memo_estimated_seconds_remaining
8468

8569
hours, minutes, seconds = timer.divide_seconds(memo_estimated_seconds_remaining)
8670

8771
if hours > OOB_LIMIT_IN_HOURS && out_of_bounds_time_format
88-
OOB_TEXT_TO_FORMAT[out_of_bounds_time_format]
72+
OOB_TEXT_TO_FORMAT.fetch(out_of_bounds_time_format)
8973
else
9074
TIME_FORMAT % [hours, minutes, seconds]
9175
end
@@ -99,8 +83,8 @@ def elapsed
9983
TIME_FORMAT % [hours, minutes, seconds]
10084
end
10185

102-
def estimated_with_elapsed_fallback
103-
progress.finished? ? elapsed_with_label : estimated_with_label
86+
def estimated_with_elapsed_fallback(out_of_bounds_time_format)
87+
progress.finished? ? elapsed_with_label : estimated_with_label(out_of_bounds_time_format)
10488
end
10589

10690
def estimated_seconds_remaining

spec/lib/ruby-progressbar/components/time_spec.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Components
1414

1515
timer.start
1616

17-
expect(time.estimated_with_label).to eql ' ETA: ??:??:??'
17+
expect(time.estimated_with_label(:unknown)).to eql ' ETA: ??:??:??'
1818
end
1919

2020
it 'does not display unknown time remaining when the timer has been started and ' \
@@ -59,7 +59,7 @@ module Components
5959

6060
progress.reset
6161

62-
expect(time.estimated_with_label).to eql ' ETA: ??:??:??'
62+
expect(time.estimated_with_label(:unknown)).to eql ' ETA: ??:??:??'
6363
end
6464

6565
it 'displays unsmoothed time remaining when progress has been made even after the ' \
@@ -94,7 +94,7 @@ module Components
9494

9595
Timecop.return
9696

97-
expect(time.estimated_with_label).to eql ' ETA: > 4 Days'
97+
expect(time.estimated_with_label(:friendly)).to eql ' ETA: > 4 Days'
9898
end
9999

100100
it 'displays estimated time of "??:??:??" when estimated time is out of bounds ' \
@@ -111,7 +111,7 @@ module Components
111111

112112
Timecop.return
113113

114-
expect(time.estimated_with_label).to eql ' ETA: ??:??:??'
114+
expect(time.estimated_with_label(:unknown)).to eql ' ETA: ??:??:??'
115115
end
116116

117117
it 'displays actual estimated time when estimated time is out of bounds and the ' \
@@ -128,7 +128,7 @@ module Components
128128

129129
Timecop.return
130130

131-
expect(time.estimated_with_label).to eql ' ETA: 100:00:00'
131+
expect(time.estimated_with_label(nil)).to eql ' ETA: 100:00:00'
132132
end
133133

134134
it 'displays smoothed estimated time properly even when taking decrements into ' \
@@ -164,7 +164,7 @@ module Components
164164

165165
progress.reset
166166

167-
expect(time.estimated_with_label).to eql ' ETA: ??:??:??'
167+
expect(time.estimated_with_label(:unknown)).to eql ' ETA: ??:??:??'
168168
end
169169

170170
it 'displays smoothed estimated time after progress has been made' do
@@ -288,13 +288,6 @@ module Components
288288
expect(time.elapsed_with_label).to eql 'Time: --:--:--'
289289
end
290290

291-
it 'raises an exception when an invalid out of bounds time format is specified' do
292-
expect { Time.new(:out_of_bounds_time_format => :foo) }.
293-
to \
294-
raise_error 'Invalid Out Of Bounds time format. Valid formats are ' \
295-
'[:unknown, :friendly, nil]'
296-
end
297-
298291
it 'displays the wall clock time as unknown when the timer has been reset' do
299292
progress = Progress.new
300293
time = Time.new(:timer => timer,

0 commit comments

Comments
 (0)