Class: CapUtil::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/cap-util/timer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, quiet = nil) ⇒ Timer

Returns a new instance of Timer.



12
13
14
15
# File 'lib/cap-util/timer.rb', line 12

def initialize(name, quiet = nil)
  @name, @start_time, @end_time, @elapsed_time = name, 0, 0, 0
  @quiet = !!(quiet == :quiet)
end

Instance Attribute Details

#elapsed_timeObject (readonly)

Returns the value of attribute elapsed_time.



10
11
12
# File 'lib/cap-util/timer.rb', line 10

def elapsed_time
  @elapsed_time
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



10
11
12
# File 'lib/cap-util/timer.rb', line 10

def end_time
  @end_time
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/cap-util/timer.rb', line 10

def name
  @name
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



10
11
12
# File 'lib/cap-util/timer.rb', line 10

def start_time
  @start_time
end

Class Method Details

.pretty_time(elapsed) ⇒ Object



6
7
8
# File 'lib/cap-util/timer.rb', line 6

def self.pretty_time(elapsed)
  "#{elapsed / 60}:#{(elapsed % 60).to_s.rjust(2, '0')}"
end

Instance Method Details

#end(time = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/cap-util/timer.rb', line 22

def end(time = nil)
  @end_time = (time || ::Time.now)
  @elapsed_time = @end_time - @start_time
  if !@quiet
    elapsed = self.class.pretty_time(@elapsed_time.to_i)
    CapUtil.say "#{CapUtil.color @name, :bold, :yellow} completed in"\
                " #{CapUtil.color elapsed, :underline, :yellow}."
  end
  @end_time
end

#start(time = nil) ⇒ Object



17
18
19
20
# File 'lib/cap-util/timer.rb', line 17

def start(time = nil)
  CapUtil.say "Starting #{CapUtil.color @name, :cyan}." if !@quiet
  @start_time = (time || ::Time.now)
end