Class: WatchMe::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/watchme/timer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Timer

Returns a new instance of Timer.



7
8
9
10
# File 'lib/watchme/timer.rb', line 7

def initialize(data = {})
  @duration = data[:duration]
  start
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



5
6
7
# File 'lib/watchme/timer.rb', line 5

def duration
  @duration
end

#start_timeObject

Returns the value of attribute start_time.



5
6
7
# File 'lib/watchme/timer.rb', line 5

def start_time
  @start_time
end

#stop_timeObject

Returns the value of attribute stop_time.



5
6
7
# File 'lib/watchme/timer.rb', line 5

def stop_time
  @stop_time
end

Class Method Details

.calculate_duration_to_s(duration_in_seconds) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/watchme/timer.rb', line 45

def self.calculate_duration_to_s(duration_in_seconds)
  return "N/A" if duration_in_seconds.nil?
  
  #TODO use sortable hash if ruby 1.9.2
  times = [ 60*60*24*365.0, 60*60*24.0, 60*60.0, 60.0, 1.0, 0.001]
  names = [ 'year','day','hour','minute','second', 'millisecond']
  
  times.each_with_index do |factor,index|
    adjusted_time = duration_in_seconds / factor
    next if (adjusted_time < 1 && factor != 0.001)
    adjusted_time = (adjusted_time * 100).round / 100.0
    adjusted_time = adjusted_time.round if adjusted_time == adjusted_time.round
    name = adjusted_time == 1 ? names[index] : names[index].pluralize
    return "#{adjusted_time} #{name}"
  end      
end

.num_increments(inc_duration, total_duration) ⇒ Object



27
28
29
30
31
32
# File 'lib/watchme/timer.rb', line 27

def self.num_increments(inc_duration,total_duration)
  inc_secs = to_seconds(inc_duration)
  total_secs = to_seconds(total_duration)*1.0
  return 0 if inc_secs == 0
  return (total_secs / inc_secs).ceil
end

.to_minutes(input, data = {}) ⇒ Object



34
35
36
37
38
# File 'lib/watchme/timer.rb', line 34

def self.to_minutes(input,data = {})
  result, has_scale = to_seconds_or_as_is(input,data)
  result /= 60 if has_scale
  result
end

.to_seconds(input, data = {}) ⇒ Object



40
41
42
43
# File 'lib/watchme/timer.rb', line 40

def self.to_seconds(input,data = {})
  result, has_scale = to_seconds_or_as_is(input,data)
  result
end

Instance Method Details

#duration_to_sObject



23
24
25
# File 'lib/watchme/timer.rb', line 23

def duration_to_s
  Timer.calculate_duration_to_s(duration)
end

#startObject



12
13
14
15
# File 'lib/watchme/timer.rb', line 12

def start
  @start_time = Time.now
  @start_time
end

#stopObject



17
18
19
20
21
# File 'lib/watchme/timer.rb', line 17

def stop
  @stop_time = Time.now
  update_duration_as_required
  @stop_time
end