Class: Tracking::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/tracking/task.rb

Overview

The class for all Task objects created by List and passed to interfaces. Holds all relevant data for Tasks, as well as methods for computing new Task data as needed. Tasks internally save data (in their appropriate types) to intance variables, while public accessors return data in strings for display.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, start_time, end_time) ⇒ Task

Creates a new Task object. Data passed into its arguments is kept (unchanged) in instance variables.

Parameters:

  • name (String)

    the tasks’s name

  • start_time (Time)

    the tasks’s start time

  • end_time (Time)

    the tasks’s end time



15
16
17
18
19
# File 'lib/tracking/task.rb', line 15

def initialize(name, start_time, end_time)
	@name = name
	@start_time = start_time
	@end_time = end_time
end

Instance Attribute Details

#nameObject

Accessor for this tasks’s name (read/write)



53
54
55
# File 'lib/tracking/task.rb', line 53

def name
  @name
end

Class Method Details

.elapsed_time_lengthString

Calculates the length of strings from Task#elapsed_time (using the current elapsed time format).

Returns:

  • (String)

    the length of strings from Task#elapsed_time



47
48
49
50
# File 'lib/tracking/task.rb', line 47

def self.elapsed_time_length
	test_task = Task.new('test', Time.now, Time.now)
	return test_task.elapsed_time.length
end

Instance Method Details

#elapsed_timeString

Calculates, formats, and returns the elapsed time of this task.

Returns:

  • (String)

    the formatted elapsed time of this task



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tracking/task.rb', line 65

def elapsed_time
	# Calculate the elapsed time and break it down into different units
	seconds = (@end_time - @start_time).floor
	minutes = hours = days = 0
	if seconds >= 60
		minutes = seconds / 60
		seconds = seconds % 60
		if minutes >= 60
			hours = minutes / 60
			minutes = minutes % 60
			if hours >= 24
				days = hours / 24
				hours = hours % 24
			end
		end
	end
	# Return a string of the formatted elapsed time
	case Config[:elapsed_format]
	when :colons
		if Config[:show_elapsed_seconds]
			return '%02d:%02d:%02d:%02d' % [days, hours, minutes, seconds]
		else
			return '%02d:%02d:%02d' % [days, hours, minutes]
		end
	when :letters
		if Config[:show_elapsed_seconds]
			return '%02dd %02dh %02dm %02ds' % [days, hours, minutes, seconds]
		else
			return '%02dd %02dh %02dm' % [days, hours, minutes]
		end
	end
end

#raw(key) ⇒ Object

Gets raw data from the task object, without doing any conversions or formatting

Parameters:

  • key (Symbol)

    the key of the desired value

Returns:

  • the value of the requested key



27
28
29
30
31
32
33
34
35
36
# File 'lib/tracking/task.rb', line 27

def raw key
	case key
	when :name
		return @name
	when :start_time
		return @start_time
	when :end_time
		return @end_time
	end
end

#start_timeString

Formats and returns the start time of this task.

Returns:

  • (String)

    the formatted start time of this task



58
59
60
# File 'lib/tracking/task.rb', line 58

def start_time
	return @start_time.strftime('%H:%M')
end

#to_sObject

Converts the task object into a string (for debugging)



39
40
41
# File 'lib/tracking/task.rb', line 39

def to_s
	return "name: #{name}; start: #{@start_time}; end: #{@end_time};"
end