Sha256: fc61e12d924e2b07478ac5955c71eeb2f3ac0800f3569afe31e72e9abaad139b

Contents?: true

Size: 1.4 KB

Versions: 20

Compression:

Stored size: 1.4 KB

Contents

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

require 'observer'

module Gemwarrior
  class Timer
    include Observable

    attr_accessor :duration_in_s, :timer_name, :background, :progress, :verbose, :command

    DEFAULTS = {
      duration_in_s: 5, 
      timer_name:    'timer', 
      background:    true,
      progress:      false,
      verbose:       false,
      command:       nil
    }

    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

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

    def stop
      puts "\n#{timer_name} ended at #{Time.now}" if verbose
      changed
      notify_observers(self.command) unless self.command.nil?
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
gemwarrior-0.15.20 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.19 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.18 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.17 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.16 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.15 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.14 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.13 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.12 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.11 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.10 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.9 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.8 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.7 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.6 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.5 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.4 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.3 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.2 lib/gemwarrior/misc/timer.rb
gemwarrior-0.15.1 lib/gemwarrior/misc/timer.rb