Created
October 15, 2012 01:45
-
-
Save BrianJoyce/3890428 to your computer and use it in GitHub Desktop.
todo list working
This file contains hidden or 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
| require_relative 'todo_class' | |
| input = ARGV | |
| action = input.slice!(0) | |
| my_task = Todo.new | |
| puts "My TODO list" | |
| puts "---------------------" | |
| my_task.list if action == "list" | |
| my_task.append(ARGV) if action == "append" | |
| my_task.prepend(ARGV) if action == "prepend" | |
| my_task.delete(ARGV) if action == "delete" | |
| my_task.complete(ARGV) if action == "complete" | |
| class Todo | |
| attr_accessor :tasks, :file | |
| def open_file(access) | |
| @file = File.open("task_list.txt", access) | |
| end | |
| def all_tasks | |
| @tasks = [] | |
| @file.each_line {|line| @tasks << line} | |
| end | |
| def close_file | |
| @file.close | |
| end | |
| def list | |
| open_file("r") | |
| all_tasks | |
| @tasks.each_with_index do |task, n| | |
| puts "#{n+1}. #{task}" | |
| end | |
| close_file | |
| end | |
| def append(new_task) | |
| open_file("a+") | |
| all_tasks | |
| @file << "\n" + new_task.join(" ") | |
| close_file | |
| list | |
| end | |
| def delete(num) | |
| open_file("r") | |
| all_tasks | |
| @tasks.delete_at(num[0].to_i-1) | |
| close_file | |
| write_back | |
| end | |
| def write_back() | |
| open_file("w") | |
| @tasks.each {|task| @file << task} | |
| close_file | |
| list | |
| end | |
| def prepend(new_task) | |
| open_file("r") | |
| all_tasks | |
| close_file | |
| @tasks.insert(0,(new_task.join(" ")) + "\n") | |
| write_back | |
| end | |
| def complete(num) | |
| open_file("r") | |
| all_tasks | |
| close_file | |
| @tasks[num[0].to_i-1].chomp! << " COMPLETE!!\n" | |
| write_back | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment