Class: Gemwarrior::Timer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



6
7
8
# File 'lib/gemwarrior/misc/timer.rb', line 6

def background
  @background
end

#duration_in_sObject

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

#progressObject

Returns the value of attribute progress.



6
7
8
# File 'lib/gemwarrior/misc/timer.rb', line 6

def progress
  @progress
end

#timer_nameObject

Returns the value of attribute timer_name.



6
7
8
# File 'lib/gemwarrior/misc/timer.rb', line 6

def timer_name
  @timer_name
end

#verboseObject

Returns the value of attribute verbose.



6
7
8
# File 'lib/gemwarrior/misc/timer.rb', line 6

def verbose
  @verbose
end

Instance Method Details

#runObject



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

#startObject



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