Class: Todo::Task

Inherits:
Object
  • Object
show all
Includes:
Comparable, Logger, Syntax
Defined in:
lib/todo/task.rb

Overview

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!")

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

Instance Method Summary collapse

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

included, #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 = extract_tags(raw)
  @contexts ||= extract_contexts(raw)
  @projects ||= extract_projects(raw)

  if Todo.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

Instance Attribute Details

#completed_onObject (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

#contextsObject (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_onObject (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

#priorityObject (readonly)

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


91
92
93
# File 'lib/todo/task.rb', line 91

def priority
  @priority
end

#projectsObject (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

#rawObject (readonly)

Returns the raw content of the original task line.

Example:

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


39
40
41
# File 'lib/todo/task.rb', line 39

def raw
  @raw
end

#tagsObject (readonly)

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


78
79
80
# File 'lib/todo/task.rb', line 78

def tags
  @tags
end

Instance Method Details

#<=>(other) ⇒ Object

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


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

#dateObject

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

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


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

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

Returns:

  • (Boolean)


178
179
180
# File 'lib/todo/task.rb', line 178

def done?
  @is_completed
end

#due_onObject

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(tags[:due]) if tags[:due] =~ /(\d{4}-\d{2}-\d{2})/
  rescue ArgumentError
    return nil
  end
end

#origObject

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

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

Returns:

  • (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.

Returns:

  • (Char)

    the new priority of the task



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.

Returns:

  • (Char)

    the new priority of the task



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

#textObject

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!"


119
120
121
# File 'lib/todo/task.rb', line 119

def text
  @text ||= extract_item_text(raw)
end

#to_sObject

Returns this task as a string.

Example:

task = Todo::Task.new("(A) 2012-12-08 Task")
task.to_s
# => "(A) 2012-12-08 Task"


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(' '),
    tags.map { |tag,val| "#{tag}:#{val}" }.join(' ')
  ].grep(String).join(' ').strip
end

#toggle!Object

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


259
260
261
# File 'lib/todo/task.rb', line 259

def toggle!
  done? ? undo! : do!
end

#undo!Object

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


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