Sha256: a8073ac65981ea5b1ae56ccabad158a7857b0a58a4c7511478d3153f65656ec3

Contents?: true

Size: 1.16 KB

Versions: 74

Compression:

Stored size: 1.16 KB

Contents

# lib/gemwarrior/misc/timer.rb
# Timer

module Gemwarrior
  class Timer
    attr_accessor :duration_in_s, :timer_name, :background, :progress, :verbose

    DEFAULTS = {
      duration_in_s: 1, 
      timer_name:    'Timer', 
      background:    false,
      progress:      false,
      verbose:       true
    }

    def initialize(options = {})
      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]
    end

    def start
      if background
        Thread.start { self.run }
      else
        self.run
      end
    end

    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
  end
end

Version data entries

74 entries across 74 versions & 1 rubygems

Version Path
gemwarrior-0.15.0 lib/gemwarrior/misc/timer.rb
gemwarrior-0.14.8 lib/gemwarrior/misc/timer.rb
gemwarrior-0.14.7 lib/gemwarrior/misc/timer.rb
gemwarrior-0.14.6 lib/gemwarrior/misc/timer.rb
gemwarrior-0.14.5 lib/gemwarrior/misc/timer.rb
gemwarrior-0.14.4 lib/gemwarrior/misc/timer.rb
gemwarrior-0.14.3 lib/gemwarrior/misc/timer.rb
gemwarrior-0.14.2 lib/gemwarrior/misc/timer.rb
gemwarrior-0.14.1 lib/gemwarrior/misc/timer.rb
gemwarrior-0.14.0 lib/gemwarrior/misc/timer.rb
gemwarrior-0.13.2 lib/gemwarrior/misc/timer.rb
gemwarrior-0.13.1 lib/gemwarrior/misc/timer.rb
gemwarrior-0.13.0 lib/gemwarrior/misc/timer.rb
gemwarrior-0.12.8 lib/gemwarrior/misc/timer.rb
gemwarrior-0.12.7 lib/gemwarrior/misc/timer.rb
gemwarrior-0.12.6 lib/gemwarrior/misc/timer.rb
gemwarrior-0.12.5 lib/gemwarrior/misc/timer.rb
gemwarrior-0.12.4 lib/gemwarrior/misc/timer.rb
gemwarrior-0.12.3 lib/gemwarrior/misc/timer.rb
gemwarrior-0.12.2 lib/gemwarrior/misc/timer.rb