Created
June 24, 2012 07:46
-
-
Save dladowitz/2982321 to your computer and use it in GitHub Desktop.
ToDo App
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
require 'docopt' | |
#------------------------------------------------------- | |
class List | |
# attr_accessor :all_tasks | |
def initialize | |
@all_tasks = [] | |
@temp_arry = [] | |
end | |
def append_task(text) | |
task1 = Task.new(text) | |
# @all_tasks << task1 | |
@file = File.open("todo.txt", "a") | |
@file << task1.text + "," + task1.creation_time.to_s + "," + task1.complete + "\n" | |
puts "Task appended to list: #{text}" | |
end | |
def prepend_task(text) | |
task1 = Task.new(text) | |
task_as_array = [] | |
task_as_array[0] = task1.text | |
task_as_array[1] = task1.creation_time | |
task_as_array[2] = task1.complete + "\n" | |
task_as_array = task_as_array.join(',') | |
@file = IO.readlines("todo.txt") | |
@file.each do |task| | |
@temp_arry << task | |
end | |
@temp_arry.insert(0, task_as_array) | |
print @temp_arry | |
@file = File.open("todo.txt", "w+") | |
@temp_arry.each do |task| | |
@file << task | |
end | |
puts "Task prepend to list: #{text}" | |
end | |
def show_all_tasks | |
puts "Here is your current Task List: \n" | |
@file = IO.readlines("todo.txt") | |
@file.each do |task| | |
puts task | |
end | |
# @all_tasks.each do |task| | |
# puts task.text | |
# end | |
end | |
def delete_task(text) | |
@file = IO.readlines("todo.txt") #Reads all lines from .txt file. Assigns to @file | |
@file.each do |task| #Iterates through @file list by task | |
task_array = task.split(',') | |
if task_array[0] == text | |
puts "Task Deleted: #{text}" | |
else | |
@temp_arry << task #Throws all tasks into @temp_arry | |
end | |
end | |
@file = File.open("todo.txt", "w+") #Opens .txt file in overwrite mode | |
@temp_arry.each do |task| #Iterates through @temp_arry by task | |
@file << task #Throws each task back into the .txt file | |
end | |
end | |
def mark_complete(text) | |
@file = IO.readlines("todo.txt") #Reads all lines from .txt file. Assigns to @file | |
@file.each do |task| #Iterates through @file list by task | |
task_array = task.split(',') | |
if task_array[0] == text | |
task_array[2] = "True" | |
@temp_arry << task_array.join(',') + "\n" | |
puts "Task Marked Completed: #{text}" | |
else | |
@temp_arry << task_array.join(',') #Throws all tasks into @temp_arry | |
end | |
end | |
@file = File.open("todo.txt", "w+") #Opens .txt file in overwrite mode | |
@temp_arry.each do |task| #Iterates through @temp_arry by task | |
@file << task #Throws each task back into the .txt file | |
end | |
end | |
end | |
#------------------------------------------------------- | |
class Task | |
attr_accessor :text, :completed_time, :creation_time, :complete | |
def initialize(text) | |
@text = text | |
@creation_time = Time.now | |
@completed_time = nil | |
@complete = 'False' | |
end | |
# def complete | |
# @completed_time = Time.now | |
# end | |
end | |
#------------------------------------------------------- | |
doc = "Usage: todoDL.rb [options] | |
--append Appends a task to the list. | |
--prepend Prepends a task to the list. | |
--delete Deletes a task to the list. | |
--show Shows the current list. | |
--complete Marks a task complete. | |
-h --help Show this. | |
-v --verbose Print more text. | |
--quiet Print less text. | |
-o FILE Specify output file [default: ./test.txt]" | |
options = Docopt(doc, version=nil, help=true) | |
if options['--append'] | |
puts "Called option -a" | |
list1 = List.new | |
list1.append_task(ARGV[0]) | |
elsif options['--prepend'] | |
puts "Called option -p" | |
list1 = List.new | |
list1.prepend_task(ARGV[0]) | |
elsif options['--delete'] | |
puts "Called option -d" | |
list1 = List.new | |
list1.delete_task(ARGV[0]) | |
elsif options['--show'] | |
puts "Called option -s" | |
list1 = List.new | |
list1.show_all_tasks | |
elsif options['--complete'] | |
puts "Called option -s" | |
list1 = List.new | |
list1.mark_complete(ARGV[0]) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment