Class: Things::Task

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/things/task.rb

Constant Summary collapse

INCOMPLETED =
0
CANCELED =
2
COMPLETED =
3

Instance Method Summary collapse

Constructor Details

#initialize(task_xml, doc) ⇒ Task

Returns a new instance of Task.



9
10
11
12
# File 'lib/things/task.rb', line 9

def initialize(task_xml, doc)
  @doc      = doc
  @xml_node = task_xml
end

Instance Method Details

#<=>(another) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/things/task.rb', line 20

def <=>(another)
  if parent? && another.parent?
    parent <=> another.parent
  else
    title <=> another.title
  end
end

#bulletObject



115
116
117
# File 'lib/things/task.rb', line 115

def bullet
  completed? ? "✓" : canceled? ? "×" : "-"
end

#canceled?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/things/task.rb', line 75

def canceled?
  status == CANCELED
end

#childrenObject



123
124
125
# File 'lib/things/task.rb', line 123

def children
  @children ||= tasks_from_ids(children_ids)
end

#children?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/things/task.rb', line 127

def children?
  children.any?
end

#children_idsObject



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

def children_ids
  ids_from_relationship('children')
end

#completed?Boolean Also known as: complete?, done?

Returns:

  • (Boolean)


62
63
64
# File 'lib/things/task.rb', line 62

def completed?
  status == COMPLETED
end

#due?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/things/task.rb', line 103

def due?
  due_date && Time.now > due_date
end

#due_dateObject



99
100
101
# File 'lib/things/task.rb', line 99

def due_date
  @due_date ||= date_attribute("datedue")
end

#incompleted?Boolean Also known as: incomplete?

Returns:

  • (Boolean)


69
70
71
# File 'lib/things/task.rb', line 69

def incompleted?
  status == INCOMPLETED
end

#notesObject



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

def notes
  @notes ||= (node = @xml_node.at("attribute[@name='content']")) &&
    Hpricot.parse(node.inner_text.gsub(/\\u3c00/, "<").gsub(/\\u3e00/, ">")).inner_text
end

#notes?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/things/task.rb', line 84

def notes?
  !!notes
end

#parentObject



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

def parent
  @parent ||= task_from_id(parent_id)
end

#parent?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/things/task.rb', line 58

def parent?
  !!parent
end

#parent_idObject



50
51
52
# File 'lib/things/task.rb', line 50

def parent_id
  id_from_relationship('parent')
end

#positionObject Also known as: index, order



92
93
94
# File 'lib/things/task.rb', line 92

def position
  @position ||= @xml_node.at("attribute[@name='index']").inner_text.to_i
end

#scheduled?Boolean

Returns:

  • (Boolean)


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

def scheduled?
  !!scheduled_date
end

#scheduled_dateObject



107
108
109
# File 'lib/things/task.rb', line 107

def scheduled_date
  @scheduled_date ||= date_attribute('tickledate')
end

#statusObject



88
89
90
# File 'lib/things/task.rb', line 88

def status
  @status ||= (node = @xml_node.at("attribute[@name='status']")) && node.inner_text.to_i
end

#tag?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/things/task.rb', line 46

def tag?(name)
  tags.include?(name)
end

#tag_idsObject



32
33
34
# File 'lib/things/task.rb', line 32

def tag_ids
  ids_from_relationship("tags")
end

#tagsObject



36
37
38
39
40
# File 'lib/things/task.rb', line 36

def tags
  @tags ||= tag_ids.map do |tag_id|
    @doc.at("##{tag_id} attribute[@name=title]").inner_text
  end
end

#tags?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/things/task.rb', line 42

def tags?
  tags.any?
end

#titleObject Also known as: to_s



14
15
16
# File 'lib/things/task.rb', line 14

def title
  @xml_node.at("attribute[@name='title']").inner_text
end

#to_xmlObject



28
29
30
# File 'lib/things/task.rb', line 28

def to_xml
  @xml_node.to_s
end