Skip to content

Instantly share code, notes, and snippets.

@idlehands
Created October 16, 2012 23:30
Show Gist options
  • Save idlehands/3902728 to your computer and use it in GitHub Desktop.
Save idlehands/3902728 to your computer and use it in GitHub Desktop.
Todo 2.0 includes changes by Tanner Welsh
require 'time'
require 'yaml'
mylist = ToDoList.open('some_list.txt')
# => new list object
mylist.save
class ToDoList
attr_accessor :todos
def initialize(file_name = 'todo.yaml')
@todos = []
@file_name = file_name
end
################### Load/Save File Methods #####################
# Notes from Tanner
# def self.from_list(filename)
# self.new()
# end
def load_todos
$/ = "\n\n"
current_file = File.open(@file_name, "r")
current_file.each do |record|
@todos << YAML::load(record)
end
@todos.flatten!
end
def save_todos
output = File.open(@file_name, "w")
output.write YAML::dump(@todos)
end
################### ToDo Methods #####################
# interface with ToDo class
def add(todo_name)
@todos << ToDo.new(todo_name)
end
def delete(identifier)
sorted_list.delete_at(identifier-1)
end
def complete(identifier)
sorted_list[identifier - 1].complete
end
def add_tags(todo, tags_to_add)
tags_to_add.each { |tag| @todos[todo-1].add_tag(tag) }
end
def change_priority(todo, priority)
todos[todo-1].change_priority(priority)
end
################### Display List Methods #####################
def display_list
sorted_list.each_with_index do |todo, index|
puts "#{index + 1}: #{todo}"
end
end
def display_incomplete_list
puts "These are the items that have not been completed yet:"
incomplete_list.each do |todo|
puts "date created: #{todo.sort_time.month}/#{todo.sort_time.day}/#{todo.sort_time.year} ~ #{todo.name}"
end
end
def display_completed_list
completed_list.each do |todo|
puts "date completed: #{todo.sort_time.month}/#{todo.sort_time.day}/#{todo.sort_time.year} : #{todo.name}"
end
end
def display_list_with_tag(tag)
puts "These todos have the tag #{tag}"
puts "-------------------------------------"
puts list_with_tag(tag)
end
def display_list_with_priority(search_priority)
puts "These todos have the priority #{search_priority}"
puts "-------------------------------------"
puts list_with_priority(search_priority)
end
################### Sorted List Methods #####################
def sorted_list
@todos.sort_by! { |todo| todo.priority }
end
def completed
@todos.select { |t| t.complete? }
end
def sort_by_time(array)
array.sort_by { |t| t.sort_time }
end
def completed_list
@todos.select { |todo| todo.complete? }.sort_by { |todo| todo.sort_time }
end
def incomplete_list
@todos.select { |todo| !todo.complete? }.sort_by { |todo| todo.sort_time }
end
def list_with_tag(tag)
@todos.select { |todo| todo.tags.include?(tag) }.sort_by { |todo| todo.date_created }
end
def list_with_priority(search_priority)
@todos.select { |todo| todo.priority == search_priority }.sort_by { |todo| todo.date_created }
end
end
class ToDo
# attr_accessor :date_created, :date_completed
attr_reader :tags
def initialize(todo_name)
@completeness = false
@name = todo_name
@date_created = Time.new
@date_completed = nil
@tags = []
@priority = 4
end
def to_s
"priority: #{priority} #{name}: #{complete_in_english}, tags: #{tags.map { |tag| tag }.join(" ")}"
# "#{name}: #{complete_in_english}, tags: #{tags.map { |tag| "#" + tag }.join(" ")}"
end
def name
@name
end
def complete?
@completeness
end
def complete
@completeness = true
@date_completed = Time.new
end
def date_completed
@date_completed
end
def date_created
@date_created
end
def priority
@priority
end
def complete_in_english
complete? ? "complete" : "incomplete"
end
def sort_time
complete? ? date_completed : date_created
end
def add_tag(new_tag)
@tags << new_tag
end
def change_priority(new_priority)
@priority = new_priority
end
end
module TodoApp
def self.run
fingers_crossed = ToDoList.new
fingers_crossed.load_todos
case ARGV[0]
when "add"
to_do_name = ARGV[1..-1].join(" ")
fingers_crossed.add(to_do_name)
when "list"
case ARGV[1]
when nil
fingers_crossed.display_list
when "-c"
fingers_crossed.display_completed_list
when "-i"
fingers_crossed.display_incomplete_list
when "-t"
fingers_crossed.display_list_with_tag(ARGV[2])
when "-p"
fingers_crossed.display_list_with_priority(ARGV[2].to_i)
else
fingers_crossed.display_list
end
when "delete"
fingers_crossed.delete(ARGV[1].to_i)
when "complete"
fingers_crossed.complete(ARGV[1].to_i)
when "tag"
fingers_crossed.add_tags(ARGV[1].to_i, ARGV[2..-1])
when "prioritize"
fingers_crossed.change_priority(ARGV[1].to_i,ARGV[2].to_i)
else
fingers_crossed.display_list
end
fingers_crossed.save_todos
end
end
TodoApp.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment