Skip to content

Instantly share code, notes, and snippets.

@arn-e
Created October 16, 2012 05:11
Show Gist options
  • Save arn-e/3897323 to your computer and use it in GitHub Desktop.
Save arn-e/3897323 to your computer and use it in GitHub Desktop.
todo list revision 1
#questions :
#1. how to check if file exists? Better way than opening with 'append' (+)?
#2. how to write to middle of file? does the entire file need to be re-written from the contents in memory?
class TodoList
def initialize()
@file_name = "todo_file.txt"
@file = File.new(@file_name,'r+')
@todo_list = {}
@count = @file.lines.count
@delim = " || "
read_file
end
def add_to_list(task)
@todo_list[@count] = task
@count += 1
write_to_file
end
def read_file
puts "reading file"
@file.each do |i|
split_line = i.split(@delim)
new_task = Task.new(split_line[1],split_line[2],split_line[3])
p new_task
end
end
def write_to_file
0.upto(@count) do |i|
puts "hello : #{@todo_list.inspect}"
@file.write("#{@count}#{@delim}#{@todo_list[i].task_name}#{@delim}#{@todo_list[i].creation_date}#{@delim}#{@todo_list[i].completed.to_s}\n")
end
end
end
class Task
attr_accessor :task_name, :creation_date, :completed, :task_num
def initialize(task_name, creation_date = Time.now, completed = false)
@task_name = task_name
@creation_date = creation_date
@completed = completed
end
end
class Main
def initialize
@command = ARGV[0]
@task_name = ARGV[1..100].join(' ')
@valid_commands = ['add','prepend','list','delete','complete']
end
def parse_command
unless @valid_commands.include?(@command)
return puts "invalid command"
end
case @command
when "add"
new_task = Task.new(@task_name)
todo_list = TodoList.new
todo_list.add_to_list(new_task)
when "prepend"
#something
when "list"
#list
when "delete"
#delete
when "complete"
#complete
end
end
end
new_main = Main.new
new_main.parse_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment