Class: Tick::Timer

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



27
28
29
30
# File 'lib/tick/timer.rb', line 27

def initialize
  self.start
  self
end

Class Attribute Details

.timersObject

Returns the value of attribute timers.



115
116
117
# File 'lib/tick/timer.rb', line 115

def timers
  @timers
end

Instance Attribute Details

#start_timeObject

Returns the value of attribute start_time.



4
5
6
# File 'lib/tick/timer.rb', line 4

def start_time
  @start_time
end

#taskObject

Returns the value of attribute task.



4
5
6
# File 'lib/tick/timer.rb', line 4

def task
  @task
end

#time_spansObject

Returns the value of attribute time_spans.



4
5
6
# File 'lib/tick/timer.rb', line 4

def time_spans
  @time_spans
end

Class Method Details

.currentObject



117
118
119
120
121
# File 'lib/tick/timer.rb', line 117

def current
  list.select{|timer|
    timer.is_running
  }.first
end

.listObject



123
124
125
# File 'lib/tick/timer.rb', line 123

def list
  timers || []
end

.start_with_task(task) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tick/timer.rb', line 127

def start_with_task(task)
  timer = list.select{|timer|
    timer.task.id == task.id
  }.first

  if timer.nil?
    timer = new
    timer.task = task
  end

  if timer.is_paused
    timer.start
  end

  timer
end

Instance Method Details

#clearObject



6
7
8
9
10
11
12
# File 'lib/tick/timer.rb', line 6

def clear
  self.start_time = nil
  self.time_spans = []
  self.class.timers.delete(self)
  self.class.timers += [] # Simplify KVO
  true
end

#displayed_timeObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tick/timer.rb', line 14

def displayed_time
  hours = self.time_elapsed_in_hours.to_i
  minutes = (self.time_elapsed_in_seconds / 60).to_i - (hours * 60)

  hours = hours.to_s
  hours = "0#{hours}" if hours.length == 1

  minutes = minutes.to_s
  minutes = "0#{minutes}" if minutes.length == 1

  "#{hours}:#{minutes}"
end

#is_pausedObject Also known as: paused, paused?, is_stopped, stopped



32
33
34
# File 'lib/tick/timer.rb', line 32

def is_paused
  self.start_time.nil?
end

#is_runningObject Also known as: running, running?, is_started, started



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

def is_running
  !self.start_time.nil?
end

#startObject Also known as: resume



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tick/timer.rb', line 48

def start
  # Stop the current timer if it exists
  current_timer = self.class.current
  current_timer.stop if current_timer

  # Start the timer and add it to the
  # list of timers if it doesn't exist
  self.start_time = Time.now
  self.class.timers ||= []
  unless self.class.timers.include?(self)
    self.class.timers += [self]
  end

  true
end

#stopObject Also known as: pause



65
66
67
68
69
# File 'lib/tick/timer.rb', line 65

def stop
  self.time_spans << Time.now - self.start_time
  self.start_time = nil
  true
end

#submit!(options = {}, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tick/timer.rb', line 72

def submit!(options={}, &block)
  dateFormatter = NSDateFormatter.new
  dateFormatter.setDateFormat(DATE_FORMAT)

  params = {
    task_id: self.task.id,
    hours: self.time_elapsed_in_hours,
    date: Time.now
  }.merge!(options)

  entry = Entry.create(params) do |result|
    self.clear
    block.call(result) if block
  end

  self
end

#time_elapsed_in_hoursObject



106
107
108
# File 'lib/tick/timer.rb', line 106

def time_elapsed_in_hours
  self.time_elapsed_in_seconds / 60 / 60
end

#time_elapsed_in_secondsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tick/timer.rb', line 90

def time_elapsed_in_seconds
  time_elapsed_in_seconds = 0

  # Add up time spans
  self.time_spans.each do |seconds|
    time_elapsed_in_seconds += seconds
  end

  # Add the current running time
  if self.start_time
    time_elapsed_in_seconds += Time.now - self.start_time
  end

  time_elapsed_in_seconds
end