Class: Todo::Task
- Inherits:
-
Object
- Object
- Todo::Task
- Defined in:
- lib/todo/task.rb
Overview
Constant Summary
Constants included from Syntax
Syntax::COMPLETED_FLAG, Syntax::COMPLETED_ON_PATTERN, Syntax::CONTEXTS_PATTERN, Syntax::CREATED_ON_PATTERN, Syntax::DUE_ON_PATTERN, Syntax::PRIORITY_PATTERN, Syntax::PROJECTS_PATTERN, Syntax::SINGLE_SPACE, Syntax::TAGS_PATTERN
Instance Attribute Summary collapse
-
#completed_on ⇒ Object
readonly
Returns the task’s completion date if task is done.
-
#contexts ⇒ Object
readonly
Returns an array of all the @context annotations.
-
#created_on ⇒ Object
readonly
Returns the task’s creation date, if any.
-
#priority ⇒ Object
readonly
Returns the priority, if any.
-
#projects ⇒ Object
readonly
Returns an array of all the +project annotations.
-
#raw ⇒ Object
readonly
Returns the raw content of the original task line.
-
#tags ⇒ Object
readonly
Returns tag annotations embedded in the list item.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares the priorities of two tasks.
-
#date ⇒ Object
Deprecated.
-
#do! ⇒ Object
Completes the task on the current date.
-
#done? ⇒ Boolean
Returns true if the task is completed.
-
#due_on ⇒ Object
Returns the task’s due date, if any.
-
#initialize(line) ⇒ Task
constructor
A new instance of Task.
-
#orig ⇒ Object
Deprecated.
-
#overdue? ⇒ Boolean
Returns whether a task’s due date is in the past.
-
#priority_dec! ⇒ Char
Decreases the priority until Z.
-
#priority_inc! ⇒ Char
Increases the priority until A.
-
#text ⇒ Object
Gets just the text content of the todo, without the priority, contexts and projects annotations.
-
#to_s ⇒ Object
Returns this task as a string.
-
#toggle! ⇒ Object
Toggles the task from complete to incomplete or vice versa.
-
#undo! ⇒ Object
Marks the task as incomplete and resets its original priority.
Methods included from Syntax
#check_completed_flag, #extract_completed_date, #extract_contexts, #extract_created_on, #extract_item_text, #extract_priority, #extract_projects, #extract_tags
Methods included from Logger
Constructor Details
#initialize(line) ⇒ Task
Returns a new instance of Task.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/todo/task.rb', line 15 def initialize(line) @raw = line @priority = extract_priority(raw) @created_on = extract_created_on(raw) @tags = (raw) @contexts ||= extract_contexts(raw) @projects ||= extract_projects(raw) if Todo..require_completed_on @completed_on = extract_completed_date(raw) @is_completed = !@completed_on.nil? else @completed_on = extract_completed_date(raw) @is_completed = check_completed_flag(raw) end end |
Instance Attribute Details
#completed_on ⇒ Object (readonly)
Returns the task’s completion date if task is done.
Example:
task = Todo::Task.new("x 2012-03-04 Task.")
task.completed_on
# => <Date: 2012-03-04 (4911981/2,0,2299161)>
Dates must be in the YYYY-MM-DD format as specified in the todo.txt format. Dates in any other format will be classed as malformed and this attribute will be nil.
65 66 67 |
# File 'lib/todo/task.rb', line 65 def completed_on @completed_on end |
#contexts ⇒ Object (readonly)
Returns an array of all the @context annotations.
Example:
task = Todo:Task.new("(A) @context Testing!")
task.context
# => ["@context"]
100 101 102 |
# File 'lib/todo/task.rb', line 100 def contexts @contexts end |
#created_on ⇒ Object (readonly)
Returns the task’s creation date, if any.
Example:
task = Todo::Task.new("(A) 2012-03-04 Task.")
task.created_on
#=> <Date: 2012-03-04 (4911981/2,0,2299161)>
Dates must be in the YYYY-MM-DD format as specified in the todo.txt format. Dates in any other format will be classed as malformed and this attribute will be nil.
52 53 54 |
# File 'lib/todo/task.rb', line 52 def created_on @created_on end |
#priority ⇒ Object (readonly)
91 92 93 |
# File 'lib/todo/task.rb', line 91 def priority @priority end |
#projects ⇒ Object (readonly)
Returns an array of all the +project annotations.
Example:
task = Todo:Task.new("(A) +test Testing!")
task.projects
# => ["+test"]
109 110 111 |
# File 'lib/todo/task.rb', line 109 def projects @projects end |
#raw ⇒ Object (readonly)
39 40 41 |
# File 'lib/todo/task.rb', line 39 def raw @raw end |
#tags ⇒ Object (readonly)
78 79 80 |
# File 'lib/todo/task.rb', line 78 def @tags end |
Instance Method Details
#<=>(other) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/todo/task.rb', line 297 def <=>(other) if priority.nil? && other.priority.nil? 0 elsif other.priority.nil? 1 elsif priority.nil? -1 else other.priority <=> priority end end |
#date ⇒ Object
Deprecated. See: #created_on
124 125 126 127 128 |
# File 'lib/todo/task.rb', line 124 def date logger.warn("`Task#date` is deprecated, use `Task#created_on` instead.") @created_on end |
#do! ⇒ Object
196 197 198 199 200 |
# File 'lib/todo/task.rb', line 196 def do! @completed_on = Date.today @is_completed = true @priority = nil end |
#done? ⇒ Boolean
178 179 180 |
# File 'lib/todo/task.rb', line 178 def done? @is_completed end |
#due_on ⇒ Object
Returns the task’s due date, if any.
Example:
task = Todo::Task.new("(A) This is a task. due:2012-03-04")
task.due_on
# => <Date: 2012-03-04 (4911981/2,0,2299161)>
Dates must be in the YYYY-MM-DD format as specified in the todo.txt format. Dates in any other format will be classed as malformed and this attribute will be nil.
148 149 150 151 152 153 154 |
# File 'lib/todo/task.rb', line 148 def due_on begin Date.parse([:due]) if [:due] =~ /(\d{4}-\d{2}-\d{2})/ rescue ArgumentError return nil end end |
#orig ⇒ Object
Deprecated. See: #raw
131 132 133 134 135 |
# File 'lib/todo/task.rb', line 131 def orig logger.warn("`Task#orig` is deprecated, use `Task#raw` instead.") raw end |
#overdue? ⇒ Boolean
163 164 165 |
# File 'lib/todo/task.rb', line 163 def overdue? !due_on.nil? && due_on < Date.today end |
#priority_dec! ⇒ Char
Decreases the priority until Z. if it’s nil, it does nothing and returns nil.
235 236 237 238 239 |
# File 'lib/todo/task.rb', line 235 def priority_dec! return if @priority.nil? @priority = @priority.next if @priority.ord < 90 @priority end |
#priority_inc! ⇒ Char
Increases the priority until A. If it’s nil, it sets it to A.
223 224 225 226 227 228 229 230 |
# File 'lib/todo/task.rb', line 223 def priority_inc! if @priority.nil? @priority = 'A' elsif @priority.ord > 65 @priority = (@priority.ord - 1).chr end @priority end |
#text ⇒ Object
119 120 121 |
# File 'lib/todo/task.rb', line 119 def text @text ||= extract_item_text(raw) end |
#to_s ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/todo/task.rb', line 270 def to_s [ done? && "x #{completed_on}", priority && "(#{priority})", created_on.to_s, text, contexts.join(' '), projects.join(' '), .map { |tag,val| "#{tag}:#{val}" }.join(' ') ].grep(String).join(' ').strip end |
#toggle! ⇒ Object
259 260 261 |
# File 'lib/todo/task.rb', line 259 def toggle! done? ? undo! : do! end |
#undo! ⇒ Object
215 216 217 218 219 |
# File 'lib/todo/task.rb', line 215 def undo! @completed_on = nil @is_completed = false @priority = extract_priority(raw) end |