Module: Tracking::List

Extended by:
List
Included in:
List
Defined in:
lib/tracking/list.rb

Overview

Tracking’s core. Contains methods for manipulating tasks in the data file and preparing its data for a user interface.

Instance Method Summary collapse

Instance Method Details

#add(name, time = Time.now) ⇒ Object

Adds a task to the list

Parameters:

  • name (String)

    the name of the task to add to the list



57
58
59
60
61
62
# File 'lib/tracking/list.rb', line 57

def add name, time=Time.now
	FileUtils.touch @data_file unless File.exist? @data_file
	File.open(@data_file, 'a') do |file|
		file << [ time.to_s, name ].to_csv(@csv_options)
	end
end

#clearObject

Clears the entire list



90
91
92
93
94
95
# File 'lib/tracking/list.rb', line 90

def clear
	if File.exist? @data_file
		FileUtils.rm @data_file
		FileUtils.touch @data_file
	end
end

#create_task_from_data(line, next_line = nil) ⇒ Task

Generates a Task object from one or two lines of semi-parsed CSV data

Parameters:

  • line (Array)

    the line of semi-parsed CSV data to use

  • next_line (Array) (defaults to: nil)

    the next line of data, if it exists

Returns:

  • (Task)

    the generated Task object



46
47
48
49
50
51
52
# File 'lib/tracking/list.rb', line 46

def create_task_from_data(line, next_line=nil)
	name = line[1]
	start_time = Time.parse line[0]
	end_time = next_line.nil? ? Time.now : Time.parse(next_line[0])

	return Task.new(name, start_time, end_time)
end

#deleteObject

Deletes the last task from the list



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tracking/list.rb', line 65

def delete
	if File.exist? @data_file
		lines = File.readlines @data_file
		lines.pop # Or delete specific lines in the future
		File.open(@data_file, 'w') do |file| 
			lines.each do |line|
				file << line
			end
		end
	end
end

#get(max = Config[:lines]) ⇒ Array

Reads part of the data file and creates Task objects from that data

the data file

Parameters:

  • max (Integer) (defaults to: Config[:lines])

    the maximum number of items to get from the end of

Returns:

  • (Array)

    an array of Task objects



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tracking/list.rb', line 24

def get max=Config[:lines]
	if File.exist? @data_file
		all_lines = CSV.read(@data_file, @csv_options)
		lines = all_lines[-max..-1]
		lines = all_lines if lines.nil?

		tasks = []
		lines.each_with_index do |line, i|
			tasks << create_task_from_data(line, lines[i+1])
		end
		return tasks
	else
		return []
	end
end

#rename(name) ⇒ Object

Renames the last task in the list

Parameters:

  • name (String)

    the new name for the last task



80
81
82
83
84
85
86
87
# File 'lib/tracking/list.rb', line 80

def rename name
	# get task data
	old_task = get(1).first
	# delete last task
	delete
	# add new task with old time
	add(name, old_task.raw(:start_time))
end