Module: Tracking::List
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
-
#add(name, time = Time.now) ⇒ Object
Adds a task to the list.
-
#clear ⇒ Object
Clears the entire list.
-
#create_task_from_data(line, next_line = nil) ⇒ Task
Generates a Task object from one or two lines of semi-parsed CSV data.
-
#delete ⇒ Object
Deletes the last task from the list.
-
#get(max = Config[:lines]) ⇒ Array
Reads part of the data file and creates Task objects from that data.
-
#rename(name) ⇒ Object
Renames the last task in the list.
Instance Method Details
#add(name, time = Time.now) ⇒ Object
Adds a task 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 |
#clear ⇒ Object
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
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 |
#delete ⇒ Object
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
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
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 |