Class: ProgressBar::Components::Rate

Inherits:
Object
  • Object
show all
Includes:
Progressable, Timer
Defined in:
lib/ruby-progressbar/components/rate.rb

Defined Under Namespace

Classes: As

Constant Summary

Constants included from Progressable

Progressable::DEFAULT_BEGINNING_POSITION, Progressable::DEFAULT_SMOOTHING, Progressable::DEFAULT_TOTAL

Constants included from Timer

Timer::TIME_FORMAT

Instance Attribute Summary collapse

Attributes included from Progressable

#autofinish, #finished, #progress, #running_average, #smoothing, #starting_position, #total

Instance Method Summary collapse

Methods included from Progressable

#decrement, #finish, #finished?, #increment, #percentage_completed, #percentage_completed_with_precision, #progress_made, #started?

Methods included from Timer

#pause, #resume, #started?, #stop, #stopped?

Constructor Details

#initialize(options = {}) ⇒ Rate

Returns a new instance of Rate.



9
10
11
12
13
14
15
# File 'lib/ruby-progressbar/components/rate.rb', line 9

def initialize(options = {})
  self.rate_scale = options[:rate_scale]
  @started_at     = nil
  @stopped_at     = nil

  super
end

Instance Attribute Details

#rate_scaleObject

Returns the value of attribute rate_scale.



7
8
9
# File 'lib/ruby-progressbar/components/rate.rb', line 7

def rate_scale
  @rate_scale
end

Instance Method Details

#resetObject



22
23
24
25
# File 'lib/ruby-progressbar/components/rate.rb', line 22

def reset
  as(Timer).reset
  as(Progressable).reset
end

#start(options = {}) ⇒ Object



17
18
19
20
# File 'lib/ruby-progressbar/components/rate.rb', line 17

def start(options = {})
  as(Timer).start
  as(Progressable).start(options)
end

#to_s(format_string = "%i") ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-progressbar/components/rate.rb', line 27

def to_s(format_string = "%i")
  elapsed = elapsed_whole_seconds.to_f
  return 0 unless elapsed > 0

  base_rate   = (progress_made / elapsed)

  if rate_scale
    scaled_rate = rate_scale.call(base_rate)
  else
    scaled_rate = base_rate
  end

  format_string % scaled_rate
end