Class: Syctask::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/syctask/task_tracker.rb

Overview

A Track holds a task and stops the time the task is processed. The Track will print every second the elapsed time and the time left to the specified Task#duration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ Track

Creates a new Track for the provided task



149
150
151
152
153
154
155
# File 'lib/syctask/task_tracker.rb', line 149

def initialize(task)
  @dir = task.dir
  @id = task.id
  @title = task.title
  @duration = task.remaining.to_i
  @semaphore = "#{Syctask::SYC_DIR}/#{@id}.track"
end

Instance Attribute Details

#dirObject (readonly)

Directory where the tracked task is saved



138
139
140
# File 'lib/syctask/task_tracker.rb', line 138

def dir
  @dir
end

#idObject (readonly)

ID of the tracked task



140
141
142
# File 'lib/syctask/task_tracker.rb', line 140

def id
  @id
end

#startedObject (readonly)

When tracking started



144
145
146
# File 'lib/syctask/task_tracker.rb', line 144

def started
  @started
end

#stoppedObject (readonly)

When tracking stopped



146
147
148
# File 'lib/syctask/task_tracker.rb', line 146

def stopped
  @stopped
end

#titleObject (readonly)

Title of the tracked task



142
143
144
# File 'lib/syctask/task_tracker.rb', line 142

def title
  @title
end

Instance Method Details

#start(show) ⇒ Object

Starts the tracking and a timer that will print to STDOUT every second the elapsed time and the time left until Task#duration



159
160
161
162
163
164
# File 'lib/syctask/task_tracker.rb', line 159

def start(show)
  @started ||= Time.now
  # start a timer that prints id and elapsed time 
  FileUtils.touch @semaphore
  system "console_timer #{@duration} #{@id} #{@semaphore} &" if show
end

#stopObject

Stops the task tracking and returns the lead time of the task



167
168
169
170
171
# File 'lib/syctask/task_tracker.rb', line 167

def stop
  FileUtils.rm @semaphore if @semaphore and File.exist? @semaphore
  @stopped ||= Time.now
  @stopped - @started
end