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_FLAG_PATTERN, 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, options = Todo.options) ⇒ 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, options = Todo.options) ⇒ Task
Returns a new instance of Task.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/todo/task.rb', line 17 def initialize(line, =Todo.) @raw = line @priority = extract_priority(raw) @created_on = extract_created_on(raw) @tags = (raw) @contexts ||= extract_contexts(raw) @projects ||= extract_projects(raw) if .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.
67 68 69 |
# File 'lib/todo/task.rb', line 67 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"]
102 103 104 |
# File 'lib/todo/task.rb', line 102 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.
54 55 56 |
# File 'lib/todo/task.rb', line 54 def created_on @created_on end |
#priority ⇒ Object (readonly)
93 94 95 |
# File 'lib/todo/task.rb', line 93 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"]
111 112 113 |
# File 'lib/todo/task.rb', line 111 def projects @projects end |
#raw ⇒ Object (readonly)
41 42 43 |
# File 'lib/todo/task.rb', line 41 def raw @raw end |
#tags ⇒ Object (readonly)
80 81 82 |
# File 'lib/todo/task.rb', line 80 def @tags end |
Instance Method Details
#<=>(other) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/todo/task.rb', line 280 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
126 127 128 129 130 |
# File 'lib/todo/task.rb', line 126 def date logger.warn("`Task#date` is deprecated, use `Task#created_on` instead.") @created_on end |
#do! ⇒ Object
198 199 200 201 202 |
# File 'lib/todo/task.rb', line 198 def do! @completed_on = Date.today @is_completed = true @priority = nil end |
#done? ⇒ Boolean
180 181 182 |
# File 'lib/todo/task.rb', line 180 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.
150 151 152 153 154 155 156 |
# File 'lib/todo/task.rb', line 150 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
133 134 135 136 137 |
# File 'lib/todo/task.rb', line 133 def orig logger.warn("`Task#orig` is deprecated, use `Task#raw` instead.") raw end |
#overdue? ⇒ Boolean
165 166 167 |
# File 'lib/todo/task.rb', line 165 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.
237 238 239 240 241 |
# File 'lib/todo/task.rb', line 237 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.
225 226 227 228 229 230 231 232 |
# File 'lib/todo/task.rb', line 225 def priority_inc! if @priority.nil? @priority = 'A' elsif @priority.ord > 65 @priority = (@priority.ord - 1).chr end @priority end |
#text ⇒ Object
121 122 123 |
# File 'lib/todo/task.rb', line 121 def text @text ||= extract_item_text(raw) end |
#to_s ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/todo/task.rb', line 299 def to_s [ print_done_marker, print_priority, created_on.to_s, text, print_contexts, print_projects, ].reject { |item| !item || item.nil? || item.empty? }.join(' ') end |
#toggle! ⇒ Object
261 262 263 |
# File 'lib/todo/task.rb', line 261 def toggle! done? ? undo! : do! end |
#undo! ⇒ Object
217 218 219 220 221 |
# File 'lib/todo/task.rb', line 217 def undo! @completed_on = nil @is_completed = false @priority = extract_priority(raw) end |