Created
June 22, 2012 05:24
-
-
Save chadwickdonald/2970457 to your computer and use it in GitHub Desktop.
Todo list
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 "docopt" | |
| require "csv" | |
| class Task | |
| attr_accessor :description, :creation_time, :completion_time, :completed | |
| def initialize(description) | |
| @description = description | |
| @creation_time = Time.new.to_s | |
| @completion_time = "" | |
| @completed = "not completed" | |
| end | |
| def in_words | |
| delim = "|" | |
| @description + delim + @creation_time.to_s + delim + @completion_time + delim + @completed + "\n" | |
| end | |
| def complete | |
| @completion_time = Time.new.to_s | |
| @completed = "completed!" | |
| end | |
| end | |
| class Todo | |
| def initialize | |
| @filename="todo_clean.txt" | |
| end | |
| def add(description, where_to) | |
| task = Task.new(description) | |
| if File.exists?(@filename) | |
| File.open(@filename, "a") {|f| f.write(task.in_words)} | |
| else | |
| File.open(@filename, "w") {|f| f.write(task.in_words)} | |
| end | |
| puts "We're adding it to the #{ where_to }" | |
| end | |
| def list(order_by) | |
| #@filename = "jerk.txt" | |
| file = File.open(@filename, "rb") | |
| contents = file.read.split("\n") | |
| contents.each do |line| | |
| puts line | |
| end | |
| puts "We're ordering by #{ order_by }" | |
| end | |
| def delete_by_word(thing_to_delete) | |
| file = File.open(@filename, "rb") | |
| contents = file.read.split("\n") | |
| contents.delete(thing_to_delete) | |
| File.open(@filename, "w") {|f| f.write(contents.join("\n"))} | |
| end | |
| def delete(line_number) | |
| # file = File.open(@filename, "rb") | |
| # lines = File.readlines(@filename) | |
| # File.open(@filename, 'w+'){|f| f.write @file.join() } | |
| # file.each do |line| | |
| # if file.lineno == line_number | |
| # # puts line.class | |
| # # puts line.methods.sort | |
| # # # line.delete! | |
| # end | |
| # end | |
| output = File.open(@filename, "r") | |
| File.open(@filename, "w") do |line| | |
| puts line.class | |
| end | |
| puts "This should delete task number #{ line_number }." | |
| end | |
| def complete(thing_to_complete) | |
| puts "This should complete #{ thing_to_complete }" | |
| # @completed = "Completed" | |
| end | |
| end | |
| todo = Todo.new | |
| doc = "Usage: ruby todo_app.rb [option] <arguments>... | |
| Options: | |
| -a TASK Adds a task to the end of your todo list. | |
| --ab TASK Adds a task to the beginning of your list. | |
| -l Print a list of your todo tasks ordered by creation date. | |
| --lc Print a list of your todo tasks ordered by completion date. | |
| -d TASK User can delete a specific task item from their todo list. | |
| -c TASK User can mark a task item in their todo list as complete. | |
| -h Prints the help documentation. | |
| -v Prints the app version. | |
| " | |
| if __FILE__ == $0 | |
| command = ARGV[0] | |
| command_arg = ARGV[1] | |
| options = Docopt(doc, '0.0.1', true) | |
| case command | |
| when '-a' then todo.add(command_arg, "end") | |
| when '--ab' then todo.add(command_arg, "begin") | |
| when '-l' then todo.list("creation date") | |
| when '--lc' then todo.list("completion date") | |
| when '-d' then todo.delete(command_arg) | |
| when '-c' then todo.complete(command_arg) | |
| else | |
| puts "Sorry, I don't know how to (#{command}). Try typing -h to get some help." | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment