Class: ProgressBar::Timer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Timer

Returns a new instance of Timer.



8
9
10
# File 'lib/ruby-progressbar/timer.rb', line 8

def initialize(options = {})
  self.time = options[:time] || ::ProgressBar::Time.new
end

Instance Attribute Details

#started_atObject

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_atObject

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_secondsObject



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_secondsObject



63
64
65
# File 'lib/ruby-progressbar/timer.rb', line 63

def elapsed_whole_seconds
  elapsed_seconds.floor
end

#nowObject



31
32
33
# File 'lib/ruby-progressbar/timer.rb', line 31

def now
  time.now
end

#pauseObject



23
24
25
# File 'lib/ruby-progressbar/timer.rb', line 23

def pause
  stop
end

#resetObject



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

Returns:

  • (Boolean)


48
49
50
# File 'lib/ruby-progressbar/timer.rb', line 48

def reset?
  !started_at
end

#restartObject



52
53
54
55
# File 'lib/ruby-progressbar/timer.rb', line 52

def restart
  reset
  start
end

#resumeObject



27
28
29
# File 'lib/ruby-progressbar/timer.rb', line 27

def resume
  start
end

#startObject



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

Returns:

  • (Boolean)


35
36
37
# File 'lib/ruby-progressbar/timer.rb', line 35

def started?
  started_at
end

#stopObject



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

Returns:

  • (Boolean)


39
40
41
# File 'lib/ruby-progressbar/timer.rb', line 39

def stopped?
  stopped_at
end