Class: CapUtil::Timer
- Inherits:
-
Object
- Object
- CapUtil::Timer
- Defined in:
- lib/cap-util/timer.rb
Instance Attribute Summary collapse
-
#elapsed_time ⇒ Object
readonly
Returns the value of attribute elapsed_time.
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
Instance Method Summary collapse
- #end(time = nil) ⇒ Object
-
#initialize(name, quiet = nil) ⇒ Timer
constructor
A new instance of Timer.
- #start(time = nil) ⇒ Object
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_time ⇒ Object (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_time ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/cap-util/timer.rb', line 10 def name @name end |
#start_time ⇒ Object (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 |