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
CHECK_MARK =
""
X_MARK =
"×"
MINUS_MARK =
"-"

Instance Method Summary collapse

Constructor Details

#initialize(task_xml, doc) ⇒ Task

Returns a new instance of Task.



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

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

Instance Method Details

#<=>(another) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/things/task.rb', line 28

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

#bulletObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/things/task.rb', line 123

def bullet
  case
  when completed?
    CHECK_MARK
  when canceled?
    X_MARK
  else
    MINUS_MARK
  end
end

#canceled?Boolean

Returns:

  • (Boolean)


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

def canceled?
  status == CANCELED
end

#childrenObject



138
139
140
# File 'lib/things/task.rb', line 138

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

#children?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/things/task.rb', line 142

def children?
  children.any?
end

#children_idsObject



134
135
136
# File 'lib/things/task.rb', line 134

def children_ids
  ids_from_relationship('children')
end

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

Returns:

  • (Boolean)


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

def completed?
  status == COMPLETED
end

#due?Boolean

Returns:

  • (Boolean)


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

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

#due_dateObject



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

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

#incompleted?Boolean Also known as: incomplete?

Returns:

  • (Boolean)


77
78
79
# File 'lib/things/task.rb', line 77

def incompleted?
  status == INCOMPLETED
end

#notesObject



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

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)


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

def notes?
  !!notes
end

#parentObject



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

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

#parent?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/things/task.rb', line 66

def parent?
  !!parent
end

#parent_idObject



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

def parent_id
  id_from_relationship('parent')
end

#positionObject Also known as: index, order



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

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

#scheduled?Boolean

Returns:

  • (Boolean)


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

def scheduled?
  !!scheduled_date
end

#scheduled_dateObject



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

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

#statusObject



96
97
98
# File 'lib/things/task.rb', line 96

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

#tag?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def tag?(name)
  tags.any? { |t| t.casecmp(name) == 0 }
end

#tag_idsObject



40
41
42
# File 'lib/things/task.rb', line 40

def tag_ids
  ids_from_relationship("tags")
end

#tagsObject



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

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

#tags?Boolean

Returns:

  • (Boolean)


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

def tags?
  tags.any?
end

#titleObject Also known as: to_s



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

def title
  if n = @xml_node.at("attribute[@name='title']")
    n.inner_text
  else
    "FAIL: [#{@xml_node}]"
  end
end

#to_xmlObject



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

def to_xml
  @xml_node.to_s
end