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] || Time
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



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

def divide_seconds(seconds)
  hours, seconds   = seconds.divmod(3600)
  minutes, seconds = seconds.divmod(60)

  [hours, minutes, seconds]
end

#elapsed_secondsObject



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

def elapsed_seconds
  ((stopped_at || time.now) - started_at)
end

#elapsed_whole_secondsObject



57
58
59
# File 'lib/ruby-progressbar/timer.rb', line 57

def elapsed_whole_seconds
  elapsed_seconds.floor
end

#pauseObject



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

def pause
  stop
end

#resetObject



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

def reset
  self.started_at = nil
  self.stopped_at = nil
end

#reset?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ruby-progressbar/timer.rb', line 44

def reset?
  !started_at
end

#restartObject



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

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)


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

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)


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

def stopped?
  stopped_at
end