Class: Tick::Timer
- Inherits:
-
Object
- Object
- Tick::Timer
- Defined in:
- lib/tick/timer.rb
Class Attribute Summary collapse
-
.timers ⇒ Object
Returns the value of attribute timers.
Instance Attribute Summary collapse
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#task ⇒ Object
Returns the value of attribute task.
-
#time_spans ⇒ Object
Returns the value of attribute time_spans.
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
- #displayed_time ⇒ Object
-
#initialize ⇒ Timer
constructor
A new instance of Timer.
- #is_paused ⇒ Object (also: #paused, #paused?, #is_stopped, #stopped)
- #is_running ⇒ Object (also: #running, #running?, #is_started, #started)
- #start ⇒ Object (also: #resume)
- #stop ⇒ Object (also: #pause)
- #submit!(options = {}, &block) ⇒ Object
- #time_elapsed_in_hours ⇒ Object
- #time_elapsed_in_seconds ⇒ Object
Constructor Details
#initialize ⇒ Timer
Returns a new instance of Timer.
26 27 28 29 |
# File 'lib/tick/timer.rb', line 26 def initialize self.start self end |
Class Attribute Details
.timers ⇒ Object
Returns the value of attribute timers.
114 115 116 |
# File 'lib/tick/timer.rb', line 114 def timers @timers end |
Instance Attribute Details
#start_time ⇒ Object
Returns the value of attribute start_time.
4 5 6 |
# File 'lib/tick/timer.rb', line 4 def start_time @start_time end |
#task ⇒ Object
Returns the value of attribute task.
4 5 6 |
# File 'lib/tick/timer.rb', line 4 def task @task end |
#time_spans ⇒ Object
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
.current ⇒ Object
116 117 118 119 120 |
# File 'lib/tick/timer.rb', line 116 def current list.select{|timer| timer.is_running }.first end |
.list ⇒ Object
122 123 124 |
# File 'lib/tick/timer.rb', line 122 def list timers || [] end |
.start_with_task(task) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/tick/timer.rb', line 126 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
#clear ⇒ Object
6 7 8 9 10 11 |
# File 'lib/tick/timer.rb', line 6 def clear self.start_time = nil self.time_spans = [] self.class.timers.delete(self) true end |
#displayed_time ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tick/timer.rb', line 13 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_paused ⇒ Object Also known as: paused, paused?, is_stopped, stopped
31 32 33 |
# File 'lib/tick/timer.rb', line 31 def is_paused self.start_time.nil? end |
#is_running ⇒ Object Also known as: running, running?, is_started, started
39 40 41 |
# File 'lib/tick/timer.rb', line 39 def is_running !self.start_time.nil? end |
#start ⇒ Object Also known as: resume
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tick/timer.rb', line 47 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 |
#stop ⇒ Object Also known as: pause
64 65 66 67 68 |
# File 'lib/tick/timer.rb', line 64 def stop self.time_spans << Time.now - self.start_time self.start_time = nil true end |
#submit!(options = {}, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tick/timer.rb', line 71 def submit!(={}, &block) dateFormatter = NSDateFormatter.new dateFormatter.setDateFormat(DATE_FORMAT) params = { task_id: self.task.id, hours: self.time_elapsed_in_hours, date: Time.now }.merge!() entry = Entry.create(params) do |result| self.clear block.call(result) if block end self end |
#time_elapsed_in_hours ⇒ Object
105 106 107 |
# File 'lib/tick/timer.rb', line 105 def time_elapsed_in_hours self.time_elapsed_in_seconds / 60 / 60 end |
#time_elapsed_in_seconds ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/tick/timer.rb', line 89 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 |