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

completed_on[R]

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.

contexts[R]

Returns an array of all the @context annotations.

Example:

task = Todo:Task.new("(A) @context Testing!")
task.context
# => ["@context"]
created_on[R]

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.

priority[R]

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
projects[R]

Returns an array of all the +project annotations.

Example:

task = Todo:Task.new("(A) +test Testing!")
task.projects
# => ["+test"]
raw[R]

Returns the raw content of the original task line.

Example:

task = Todo::Task.new("(A) @context +project Hello!")
task.raw
# => "(A) @context +project Hello!"
tags[R]

Returns tag annotations embedded in the list item.

Example:

task = Todo::Task.new("Some task. due:2016-06-16 hello:world")
task.tags
# => { :due => '2016-06-16', :hello => 'world' }

task = Todo::Task.new("Some task.")
task.tags.empty?
# => true

Public Class Methods

new(line, options=Todo.options) click to toggle source

@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

<=>(other) click to toggle source

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
date() click to toggle source

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
do!() click to toggle source

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
done?() click to toggle source

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
due_on() click to toggle source

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
orig() click to toggle source

Deprecated. See: raw

# File lib/todo/task.rb, line 133
def orig
  logger.warn("`Task#orig` is deprecated, use `Task#raw` instead.")

  raw
end
overdue?() click to toggle source

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
priority_dec!() click to toggle source

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
priority_inc!() click to toggle source

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
text() click to toggle source

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
to_s() click to toggle source

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
toggle!() click to toggle source

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
undo!() click to toggle source

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

print_contexts() click to toggle source
print_done_marker() click to toggle source
print_priority() click to toggle source
print_projects() click to toggle source
print_tags() click to toggle source