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_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

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, options = Todo.options) ⇒ Task

Returns a new instance of Task.

Parameters:

  • line (String)
  • options (Todo::Options) (defaults to: Todo.options)


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, 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

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.



67
68
69
# File 'lib/todo/task.rb', line 67

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


102
103
104
# File 'lib/todo/task.rb', line 102

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.



54
55
56
# File 'lib/todo/task.rb', line 54

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


93
94
95
# File 'lib/todo/task.rb', line 93

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


111
112
113
# File 'lib/todo/task.rb', line 111

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


41
42
43
# File 'lib/todo/task.rb', line 41

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


80
81
82
# File 'lib/todo/task.rb', line 80

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


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

#dateObject

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

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


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

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)


180
181
182
# File 'lib/todo/task.rb', line 180

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.



150
151
152
153
154
155
156
# 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

#origObject

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

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)


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.

Returns:

  • (Char)

    the new priority of the task



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.

Returns:

  • (Char)

    the new priority of the task



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

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


121
122
123
# File 'lib/todo/task.rb', line 121

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"


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,
    print_tags
  ].reject { |item| !item || item.nil? || item.empty? }.join(' ')
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


261
262
263
# File 'lib/todo/task.rb', line 261

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


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