Created
June 21, 2012 22:39
-
-
Save rurabe/2969048 to your computer and use it in GitHub Desktop.
To Do
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Task | |
attr_accessor :unique_id, :description, :creation_time, :completion_time | |
def initialize(description,task_list) | |
@description = description | |
@unique_id = task_list.task_counter | |
@creation_time = Time.now | |
@completion_time = nil | |
task_list.add(self) | |
end | |
def complete | |
@completion_time = Time.now | |
end | |
end | |
class Task_list | |
attr_accessor :contents, :task_counter | |
def initialize | |
@task_counter = 1 | |
@file = File.open(File.expand_path("../file.txt",__FILE__),"w") | |
@contents = [] | |
end | |
def add(task) | |
@contents << task | |
@task_counter += 1 | |
end | |
def delete(unique_id_to_delete) | |
@contents.each_with_index do |task,index| | |
if task.unique_id == unique_id_to_delete | |
@contents.delete(task) | |
return task | |
end | |
end | |
end | |
def output_to_file | |
File.open(File.expand_path("../file.txt",__FILE__),"w") do |file| | |
file << "Task ID\tTask \#\tDescription\tCreated At\t\tCompleted At\n" | |
@contents.each_with_index do |task,index| | |
file << "#{task.unique_id}\t#{index+1}\t#{task.description}\t#{task.creation_time}\t\t#{task.completion_time}\n" | |
end | |
end | |
end | |
def import_from_file | |
@contents = [] | |
input = File.open(File.expand_path("../file.txt",__FILE__),"r") | |
input_array = [] | |
input.each do |line| | |
input_array << line.gsub("\n","").split("\t") | |
end | |
input_array.delete(input_array[0]) | |
input_array.each do |line| | |
task = Task.new(line[2],self) | |
task.unique_id = line[0] | |
task.creation_time = line[3] | |
task.completion_time = line[4] | |
end | |
end | |
def list_by_creation_time | |
contents_by_creation = @contents.sort_by {|task| task.creation_time } | |
contents_by_creation.each_with_index do |task| | |
puts "#{task.unique_id}\t#{index+1}\t#{task.description}\t#{task.creation_time}\t\t#{task.completion_time}\n" | |
end | |
end | |
end | |
tl1 = Task_list.new | |
p tl1 | |
breakfast = Task.new("eat breakfast",tl1) | |
lunch = Task.new("eat lunch",tl1) | |
dinner = Task.new("eat dinner",tl1) | |
p tl1.contents | |
#task.creation_time.strftime(%Q(%b %d, %l:%M %p)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment