public
Description: StrokeDB is an embeddable distributed document database written in Ruby
Homepage: http://strokedb.com/
Clone URL: git://github.com/yrashk/strokedb.git
oleganza (author)
Sun Jun 08 12:30:38 -0700 2008
commit  99d9135708328a0b0689d1c899d55840254bcfa1
tree    6d9fb8c7249774ac5d2a0455a89f7ee8803cdaad
parent  9db532176ee4f6c1f0451529faac869a4284ed5c
strokedb / examples / todo.rb
100755 93 lines (77 sloc) 2.118 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/env ruby
$:.unshift File.dirname(__FILE__) + "/../lib"
require "strokedb"
 
StrokeDB::Config.build :default => true, :base_path => '.todo.strokedb'
 
 
TodoItem = StrokeDB::Meta.new do
  def done!
    self.done = true
    save!
  end
  def to_s
    status = done ? "X" : " "
    "[#{status}] #{description}"
  end
end
 
TodoList = StrokeDB::Meta.new do
  has_many :items, :through => :todo_items
 
  def to_s
    s = "#{name}:\n"
    items.each do |item|
      s << " #{item}\n"
    end
    s
  end
end
 
 
def add_issue(prefix,description)
  todo_list = TodoList.find_or_create(:name => prefix)
  todo_item = TodoItem.find_or_create(:description => description, :done => false, :todo_list => todo_list)
  list_issues
end
 
def complete_issue(prefix,description)
  todo_list = TodoList.find_or_create(:name => prefix)
  return unless todo_list
  if item = todo_list.items.first(:description => description)
    item.done!
    list_issues
  else
    puts "No such item found"
  end
end
 
def list_issues
  todo_lists = TodoList.find
  return [] if todo_lists.empty?
  todo_lists.each { |list| puts list }
end
 
def extract_prefix_item(str)
  _, prefix, _, item = str.match(/(^\[(.*)\])?(.+)/).to_a
  prefix ||= "[Main]"
  prefix.gsub!(/(^\[|\]$)/,'')
  item.lstrip!
  [prefix,item]
end
 
if ARGV.empty?
  if list_issues.empty?
    puts "Type --help for program help"
  end
  exit
end
 
if ARGV.first.downcase == "--help"
  puts %{
Usage: todo Do this Add item to 'Main' list
todo [project] Do that Add item to 'project' list
todo -d Do this Complete item in 'Main' list
todo -d [project] Do that Complete item in 'project' lis
todo List all items
todo --help This help message
}
  exit
end
 
unless ARGV.first.downcase == "-d"
  prefix,item = extract_prefix_item(ARGV.join(' '))
  add_issue(prefix,item)
else
  args = ARGV
  args.shift
  prefix,item = extract_prefix_item(args.join(' '))
  complete_issue(prefix,item)
end