Class: ProgressBar::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-progressbar/throttle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Throttle

Returns a new instance of Throttle.



8
9
10
11
12
13
# File 'lib/ruby-progressbar/throttle.rb', line 8

def initialize(options = {})
  self.rate       = options[:throttle_rate] || 0.01
  self.started_at = nil
  self.stopped_at = nil
  self.timer      = options.fetch(:throttle_timer, Timer.new)
end

Instance Attribute Details

#rateObject

Returns the value of attribute rate.



3
4
5
# File 'lib/ruby-progressbar/throttle.rb', line 3

def rate
  @rate
end

#started_atObject

Returns the value of attribute started_at.



3
4
5
# File 'lib/ruby-progressbar/throttle.rb', line 3

def started_at
  @started_at
end

#stopped_atObject

Returns the value of attribute stopped_at.



3
4
5
# File 'lib/ruby-progressbar/throttle.rb', line 3

def stopped_at
  @stopped_at
end

#timerObject

Returns the value of attribute timer.



3
4
5
# File 'lib/ruby-progressbar/throttle.rb', line 3

def timer
  @timer
end

Instance Method Details

#choke(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/ruby-progressbar/throttle.rb', line 15

def choke(options = {})
  return unless !timer.started?                        ||
                options.fetch(:force_update_if, false) ||
                timer.elapsed_seconds >= rate

  timer.restart

  yield
end