class Gemwarrior::Timer

Constants

DEFAULTS

Attributes

background[RW]
command[RW]
duration_in_s[RW]
progress[RW]
timer_name[RW]
verbose[RW]

Public Class Methods

new(options = {}, repl) click to toggle source
# File lib/gemwarrior/misc/timer.rb, line 21
def initialize(options = {}, repl)
  options = DEFAULTS.merge(options)

  self.duration_in_s  = options[:duration_in_s]
  self.timer_name     = options[:timer_name]
  self.background     = options[:background]
  self.progress       = options[:progress]
  self.verbose        = options[:verbose]
  self.command        = options[:command]

  add_observer(repl)
end

Public Instance Methods

run() click to toggle source
# 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() click to toggle source
# File lib/gemwarrior/misc/timer.rb, line 34
def start
  if background
    Thread.start { self.run }
  else
    self.run
  end
end
stop() click to toggle source
# 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