class Todo::Task
Creates a new task. The argument that you pass in must be the string representation of a task.
Example:
task = Todo::Task.new("(A) A high priority task!")
Attributes
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.
Returns an array of all the @context annotations.
Example:
task = Todo:Task.new("(A) @context Testing!") task.context # => ["@context"]
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.
Returns the priority, if any.
Example:
task = Todo::Task.new("(A) Some task.") task.priority # => "A" task = Todo::Task.new "Some task." task.priority # => nil
Returns an array of all the +project annotations.
Example:
task = Todo:Task.new("(A) +test Testing!") task.projects # => ["+test"]
Returns the raw content of the original task line.
Example:
task = Todo::Task.new("(A) @context +project Hello!") task.raw # => "(A) @context +project Hello!"
Public Class Methods
@param line [String] @param options [Todo::Options]
# File lib/todo/task.rb, line 17 def initialize(line, options=Todo.options) @raw = line @priority = extract_priority(raw) @created_on = extract_created_on(raw) @tags = extract_tags(raw) @contexts ||= extract_contexts(raw) @projects ||= extract_projects(raw) if options.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
Public Instance Methods
Compares the priorities of two tasks.
Example:
task1 = Todo::Task.new("(A) Priority A.") task2 = Todo::Task.new("(B) Priority B.") task1 > task2 # => true task1 == task2 # => false task2 > task1 # => false
# 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
Deprecated. See: created_on
# File lib/todo/task.rb, line 126 def date logger.warn("`Task#date` is deprecated, use `Task#created_on` instead.") @created_on end
Completes the task on the current date.
Example:
task = Todo::Task.new("2012-12-08 Task.") task.done? # => false # Complete the task task.do! task.done? # => true
# File lib/todo/task.rb, line 198 def do! @completed_on = Date.today @is_completed = true @priority = nil end
Returns true if the task is completed.
Example:
task = Todo::Task.new("x 2012-12-08 Task.") task.done? # => true task = Todo::Task.new("Task.") task.done? # => false
# File lib/todo/task.rb, line 180 def done? @is_completed end
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.
# File lib/todo/task.rb, line 150 def due_on begin Date.parse(tags[:due]) if tags[:due] =~ /(\d{4}-\d{2}-\d{2})/ rescue ArgumentError return nil end end
Deprecated. See: raw
# File lib/todo/task.rb, line 133 def orig logger.warn("`Task#orig` is deprecated, use `Task#raw` instead.") raw end
Returns whether a task's due date is in the past.
Example:
task = Todo::Task.new("This task is overdue! due:#{Date.today - 1}") task.overdue? # => true
# File lib/todo/task.rb, line 165 def overdue? !due_on.nil? && due_on < Date.today end
Decreases the priority until Z. if it's nil, it does nothing and returns nil. @return [Char] the new priority of the task
# File lib/todo/task.rb, line 237 def priority_dec! return if @priority.nil? @priority = @priority.next if @priority.ord < 90 @priority end
Increases the priority until A. If it's nil, it sets it to A. @return [Char] the new priority of the task
# 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
Gets just the text content of the todo, without the priority, contexts and projects annotations.
Example:
task = Todo::Task.new("(A) @test Testing!") task.text # => "Testing!"
# File lib/todo/task.rb, line 121 def text @text ||= extract_item_text(raw) end
Returns this task as a string.
Example:
task = Todo::Task.new("(A) 2012-12-08 Task") task.to_s # => "(A) 2012-12-08 Task"
# File lib/todo/task.rb, line 299 def to_s [ print_done_marker, print_priority, created_on.to_s, text, print_contexts, print_projects, print_tags ].reject { |item| !item || item.nil? || item.empty? }.join(' ') end
Toggles the task from complete to incomplete or vice versa.
Example:
task = Todo::Task.new("x 2012-12-08 Task.") task.done? # => true # Toggle between complete and incomplete task.toggle! task.done? # => false task.toggle! task.done? # => true
# File lib/todo/task.rb, line 261 def toggle! done? ? undo! : do! end
Marks the task as incomplete and resets its original priority.
Example:
task = Todo::Task.new("x 2012-12-08 2012-03-04 Task.") task.done? # => true # Undo the completed task task.undo! task.done? # => false
# File lib/todo/task.rb, line 217 def undo! @completed_on = nil @is_completed = false @priority = extract_priority(raw) end
Private Instance Methods
# File lib/todo/task.rb, line 329 def print_contexts contexts.join(' ') end
# File lib/todo/task.rb, line 313 def print_done_marker return unless done? if completed_on.nil? COMPLETED_FLAG else "#{COMPLETED_FLAG} #{completed_on}" end end
# File lib/todo/task.rb, line 323 def print_priority return unless priority "(#{priority})" end
# File lib/todo/task.rb, line 333 def print_projects projects.join(' ') end