Class: Gemwarrior::Timer
- Inherits:
-
Object
- Object
- Gemwarrior::Timer
- Includes:
- Observable
- Defined in:
- lib/gemwarrior/misc/timer.rb
Constant Summary collapse
- DEFAULTS =
{ duration_in_s: 5, timer_name: 'timer', background: true, progress: false, verbose: false, command: nil }
Instance Attribute Summary collapse
-
#background ⇒ Object
Returns the value of attribute background.
-
#command ⇒ Object
Returns the value of attribute command.
-
#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 = {}, repl) ⇒ Timer
constructor
A new instance of Timer.
- #run ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options = {}, repl) ⇒ Timer
Returns a new instance of Timer.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gemwarrior/misc/timer.rb', line 21 def initialize( = {}, repl) = DEFAULTS.merge() self.duration_in_s = [:duration_in_s] self.timer_name = [:timer_name] self.background = [:background] self.progress = [:progress] self.verbose = [:verbose] self.command = [:command] add_observer(repl) end |
Instance Attribute Details
#background ⇒ Object
Returns the value of attribute background.
10 11 12 |
# File 'lib/gemwarrior/misc/timer.rb', line 10 def background @background end |
#command ⇒ Object
Returns the value of attribute command.
10 11 12 |
# File 'lib/gemwarrior/misc/timer.rb', line 10 def command @command end |
#duration_in_s ⇒ Object
Returns the value of attribute duration_in_s.
10 11 12 |
# File 'lib/gemwarrior/misc/timer.rb', line 10 def duration_in_s @duration_in_s end |
#progress ⇒ Object
Returns the value of attribute progress.
10 11 12 |
# File 'lib/gemwarrior/misc/timer.rb', line 10 def progress @progress end |
#timer_name ⇒ Object
Returns the value of attribute timer_name.
10 11 12 |
# File 'lib/gemwarrior/misc/timer.rb', line 10 def timer_name @timer_name end |
#verbose ⇒ Object
Returns the value of attribute verbose.
10 11 12 |
# File 'lib/gemwarrior/misc/timer.rb', line 10 def verbose @verbose end |
Instance Method Details
#run ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/gemwarrior/misc/timer.rb', line 42 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 return stop end end end |
#start ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/gemwarrior/misc/timer.rb', line 34 def start if background Thread.start { self.run } else self.run end end |
#stop ⇒ Object
56 57 58 59 60 |
# File 'lib/gemwarrior/misc/timer.rb', line 56 def stop puts "\n#{timer_name} ended at #{Time.now}" if verbose changed notify_observers(self.command) unless self.command.nil? end |