Class: ProgressBar::Timer
- Inherits:
-
Object
- Object
- ProgressBar::Timer
- Defined in:
- lib/ruby-progressbar/timer.rb
Instance Attribute Summary collapse
-
#started_at ⇒ Object
Returns the value of attribute started_at.
-
#stopped_at ⇒ Object
Returns the value of attribute stopped_at.
Instance Method Summary collapse
- #divide_seconds(seconds) ⇒ Object
- #elapsed_seconds ⇒ Object
- #elapsed_whole_seconds ⇒ Object
-
#initialize(options = {}) ⇒ Timer
constructor
A new instance of Timer.
- #now ⇒ Object
- #pause ⇒ Object
- #reset ⇒ Object
- #reset? ⇒ Boolean
- #restart ⇒ Object
- #resume ⇒ Object
- #start ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
- #stopped? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Timer
Returns a new instance of Timer.
8 9 10 |
# File 'lib/ruby-progressbar/timer.rb', line 8 def initialize( = {}) self.time = [:time] || ::ProgressBar::Time.new end |
Instance Attribute Details
#started_at ⇒ Object
Returns the value of attribute started_at.
5 6 7 |
# File 'lib/ruby-progressbar/timer.rb', line 5 def started_at @started_at end |
#stopped_at ⇒ Object
Returns the value of attribute stopped_at.
5 6 7 |
# File 'lib/ruby-progressbar/timer.rb', line 5 def stopped_at @stopped_at end |
Instance Method Details
#divide_seconds(seconds) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/ruby-progressbar/timer.rb', line 67 def divide_seconds(seconds) hours, seconds = seconds.divmod(3600) minutes, seconds = seconds.divmod(60) [hours, minutes, seconds] end |
#elapsed_seconds ⇒ Object
57 58 59 60 61 |
# File 'lib/ruby-progressbar/timer.rb', line 57 def elapsed_seconds return 0 unless started? ((stopped_at || time.now) - started_at) end |
#elapsed_whole_seconds ⇒ Object
63 64 65 |
# File 'lib/ruby-progressbar/timer.rb', line 63 def elapsed_whole_seconds elapsed_seconds.floor end |
#now ⇒ Object
31 32 33 |
# File 'lib/ruby-progressbar/timer.rb', line 31 def now time.now end |
#pause ⇒ Object
23 24 25 |
# File 'lib/ruby-progressbar/timer.rb', line 23 def pause stop end |
#reset ⇒ Object
43 44 45 46 |
# File 'lib/ruby-progressbar/timer.rb', line 43 def reset self.started_at = nil self.stopped_at = nil end |
#reset? ⇒ Boolean
48 49 50 |
# File 'lib/ruby-progressbar/timer.rb', line 48 def reset? !started_at end |
#restart ⇒ Object
52 53 54 55 |
# File 'lib/ruby-progressbar/timer.rb', line 52 def restart reset start end |
#resume ⇒ Object
27 28 29 |
# File 'lib/ruby-progressbar/timer.rb', line 27 def resume start end |
#start ⇒ Object
12 13 14 15 |
# File 'lib/ruby-progressbar/timer.rb', line 12 def start self.started_at = stopped? ? time.now - (stopped_at - started_at) : time.now self.stopped_at = nil end |
#started? ⇒ Boolean
35 36 37 |
# File 'lib/ruby-progressbar/timer.rb', line 35 def started? started_at end |
#stop ⇒ Object
17 18 19 20 21 |
# File 'lib/ruby-progressbar/timer.rb', line 17 def stop return unless started? self.stopped_at = time.now end |
#stopped? ⇒ Boolean
39 40 41 |
# File 'lib/ruby-progressbar/timer.rb', line 39 def stopped? stopped_at end |