Skip to content

Instantly share code, notes, and snippets.

@jerodsanto
Created May 22, 2010 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerodsanto/409729 to your computer and use it in GitHub Desktop.
Save jerodsanto/409729 to your computer and use it in GitHub Desktop.
A Simple ToDo App Using Ruby and MongoDB
#!/usr/bin/env ruby
require 'rubygems'
require 'mongo'
class ToDo
def initialize(args)
@args = args
@mongo = Mongo::Connection.new.db("todo").collection("todos")
end
def run
if @args.empty?
show :complete => false
else
case @args.first
when "help" then help
when "high" then add "high"
when "low" then add "low"
when "next" then show :complete => false, :level => "high"
when "done" then show :complete => true
when "dont" then complete false
when "finish" then complete true
else
help
end
end
end
def show(params)
sort = [["level", Mongo::ASCENDING], ["added", Mongo::ASCENDING]]
@mongo.find(params, :sort => sort).each { |todo| puts "#{todo["task"]} (#{todo["level"]})" }
end
def add(level)
help unless @args.length == 2
@mongo.insert "task" => @args.last, "level" => level, "complete" => false, "added" => Time.now
puts "added #{level} level task: #{@args.last}"
end
def complete(finish)
help unless @args.length == 2
if (document = @mongo.find_one("task" => @args.last, "complete" => false))
if finish
document["complete"] = Time.now
@mongo.save(document)
puts "finished: #{@args.last}"
else
@mongo.remove(document)
puts "removed: #{@args.last}"
end
else
puts "No ToDo matching #{@args.length} found"
end
end
def help
abort <<-HELP
usage: #{__FILE__} <method>
== Methods ==
<none> list all incomplete tasks sorted by priority then chronologically
help show this help
next list all incomplete tasks that are high priority
done list all complete tasks chronologically
high "argument" add high priority task called argument
low "argument" add low priority task called argument
finish "argument" complete task called argument
dont "argument" delete unfinished task called argument
HELP
end
end
ToDo.new(ARGV).run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment