Class: Gemwarrior::Timer
- Inherits:
-
Object
- Object
- Gemwarrior::Timer
- Defined in:
- lib/gemwarrior/misc/timer.rb
Constant Summary collapse
- DEFAULTS =
{ duration_in_s: 1, timer_name: 'Timer', background: false, progress: false, verbose: true }
Instance Attribute Summary collapse
-
#background ⇒ Object
Returns the value of attribute background.
-
#duration_in_s ⇒ Object
Returns the value of attribute duration_in_s.
-
#progress ⇒ Object
Returns the value of attribute progress.
-
#timer_name ⇒ Object
Returns the value of attribute timer_name.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Timer
constructor
A new instance of Timer.
- #run ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Timer
Returns a new instance of Timer.
16 17 18 19 20 21 22 23 24 |
# File 'lib/gemwarrior/misc/timer.rb', line 16 def initialize( = {}) = DEFAULTS.merge() self.duration_in_s = [:duration_in_s] self.timer_name = [:timer_name] self.background = [:background] self.progress = [:progress] self.verbose = [:verbose] end |
Instance Attribute Details
#background ⇒ Object
Returns the value of attribute background.
6 7 8 |
# File 'lib/gemwarrior/misc/timer.rb', line 6 def background @background end |
#duration_in_s ⇒ Object
Returns the value of attribute duration_in_s.
6 7 8 |
# File 'lib/gemwarrior/misc/timer.rb', line 6 def duration_in_s @duration_in_s end |
#progress ⇒ Object
Returns the value of attribute progress.
6 7 8 |
# File 'lib/gemwarrior/misc/timer.rb', line 6 def progress @progress end |
#timer_name ⇒ Object
Returns the value of attribute timer_name.
6 7 8 |
# File 'lib/gemwarrior/misc/timer.rb', line 6 def timer_name @timer_name end |
#verbose ⇒ Object
Returns the value of attribute verbose.
6 7 8 |
# File 'lib/gemwarrior/misc/timer.rb', line 6 def verbose @verbose end |
Instance Method Details
#run ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gemwarrior/misc/timer.rb', line 34 def run puts "#{timer_name} began at #{Time.now} for #{duration_in_s} seconds" if verbose end_time = Time.now + duration_in_s loop do sleep 1 print '.' if progress if Time.now >= end_time print "\n" puts "#{timer_name} ended at #{Time.now}" if verbose return end end end |
#start ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/gemwarrior/misc/timer.rb', line 26 def start if background Thread.start { self.run } else self.run end end |