Class: Tdo::Tasks
- Inherits:
-
Object
- Object
- Tdo::Tasks
- Defined in:
- lib/tdo.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
Instance Method Summary collapse
-
#add(task, group = nil) ⇒ Boolean
Adds the task to the list.
-
#clear ⇒ Integer
Deletes all tasks marked as done.
-
#find(str, group = nil) ⇒ Array
Finds tasks which contain the string to look for.
-
#group?(name) ⇒ Boolean
Tests whether the group exists.
-
#initialize ⇒ Tasks
constructor
Creates a new Tasks object and loads in the TODO_FILE.
-
#inspect ⇒ Object
Override default insepct.
-
#mark_done(id, group = nil) ⇒ Object
Marks the selected item as done.
-
#read ⇒ Object
Reads TODO_FILE and converts it to a hash.
-
#to_s(group = nil) ⇒ String
Converts the tasks to a string which can then be written to a file.
-
#write ⇒ Object
Writes the tasks to a file.
Constructor Details
#initialize ⇒ Tasks
Creates a new Tasks object and loads in the TODO_FILE
35 36 37 38 |
# File 'lib/tdo.rb', line 35 def initialize @items = {'ungrouped' => []} self.read end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
32 33 34 |
# File 'lib/tdo.rb', line 32 def items @items end |
Instance Method Details
#add(task, group = nil) ⇒ Boolean
Adds the task to the list
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/tdo.rb', line 61 def add(task, group=nil) if group.nil? group = 'ungrouped' else group = Tdo.get_group(group) end if self.find(task, group) false else @items[group] ||= [] @items[group] << task.strip self.write true end end |
#clear ⇒ Integer
Deletes all tasks marked as done
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/tdo.rb', line 117 def clear r = 0 @items.each do |g, ts| s = ts.size ts.delete_if {|i| i.include?(' #done') } r += s - ts.size end @items.delete_if {|g, ts| ts == [] } self.write r end |
#find(str, group = nil) ⇒ Array
find within group
Finds tasks which contain the string to look for
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/tdo.rb', line 135 def find(str, group=nil) r = [] @items.each do |g, ts| ts.each do |t| r << t if t.include?(str) end end if r == [] false else r end end |
#group?(name) ⇒ Boolean
Tests whether the group exists
187 188 189 190 191 192 193 |
# File 'lib/tdo.rb', line 187 def group?( name ) if self.items.has_key?( Tdo.get_group(name) ) true else raise InvalidGroup, "'#{name}' is not a valid group name", caller end end |
#inspect ⇒ Object
Override default insepct
196 197 198 |
# File 'lib/tdo.rb', line 196 def inspect "#<Tdo::Tasks>" end |
#mark_done(id, group = nil) ⇒ Object
Marks the selected item as done
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/tdo.rb', line 82 def mark_done(id, group=nil) unless group.nil? group = Tdo.get_group(group) self.group?(group) end if id.is_a? Integer if group @items[group][id] += ' #done' else # should really count across groups, another time! @items['ungrouped'][id] += ' #done' end elsif id.is_a? String if group @items[group].collect! { |t| t.include?(id) ? t += ' #done' : t } else # should test in every group as above @items['ungrouped'].collect! { |t| t.include?(id) ? t += ' #done' : t } end else # error return false end self.write true end |
#read ⇒ Object
Reads TODO_FILE and converts it to a hash
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tdo.rb', line 41 def read t = File.new(TODO_FILE, "r").read _group = '' t.split("\n").each do |l| if l[0] == '-' @items['ungrouped'] << l[1..-1].strip elsif l[0] == '@' _group = l[1..-1].strip @items[_group] = [] elsif l[0..1] == ' -' @items[_group] << l[2..-1].strip end end end |
#to_s(group = nil) ⇒ String
Converts the tasks to a string which can then be written to a file
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/tdo.rb', line 153 def to_s(group=nil) r = '' if group self.group?(group) group = Tdo.get_group(group) @items[group].each do |t| r += "- #{t}\n" end else @items.each do |g, ts| if g == 'ungrouped' ts.each do |t| r += "- #{t}\n" end else r += "\n@#{g}\n" ts.each do |t| r += " - #{t}\n" end end end end r end |
#write ⇒ Object
Writes the tasks to a file
179 180 181 |
# File 'lib/tdo.rb', line 179 def write File.open(TODO_FILE, "w") {|f| f.write( self.to_s )} end |