Class: Tick::Timer
- Inherits:
-
Object
- Object
- Tick::Timer
- Defined in:
- lib/tick/timer.rb
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, #is_stopped, #stopped)
- #is_running ⇒ Object (also: #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 |
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
110 111 112 113 114 |
# File 'lib/tick/timer.rb', line 110 def self.current list.select{|timer| timer.is_running }.first end |
.list ⇒ Object
116 117 118 |
# File 'lib/tick/timer.rb', line 116 def self.list @@list ||= [] end |
.start_with_task(task) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/tick/timer.rb', line 120 def self.start_with_task(task) timer = list.select{|timer| timer.task.id == task.id }.first if timer.nil? timer = new timer.task = task 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.list.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, 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, is_started, started
38 39 40 |
# File 'lib/tick/timer.rb', line 38 def is_running !self.start_time.nil? end |
#start ⇒ Object Also known as: resume
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tick/timer.rb', line 45 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 unless self.class.list.include?(self) self.class.list << self end true end |
#stop ⇒ Object Also known as: pause
61 62 63 64 65 |
# File 'lib/tick/timer.rb', line 61 def stop self.time_spans << Time.now - self.start_time self.start_time = nil true end |
#submit!(options = {}, &block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/tick/timer.rb', line 68 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
102 103 104 |
# File 'lib/tick/timer.rb', line 102 def time_elapsed_in_hours self.time_elapsed_in_seconds / 60 / 60 end |
#time_elapsed_in_seconds ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/tick/timer.rb', line 86 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 |