Class: Taskmeister::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/taskmeister/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, id, notes) ⇒ Task

Returns a new instance of Task.



7
8
9
# File 'lib/taskmeister/task.rb', line 7

def initialize(text, id, notes)
  @text, @id, @notes = text, id, notes
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/taskmeister/task.rb', line 5

def id
  @id
end

#notesObject (readonly)

Returns the value of attribute notes.



5
6
7
# File 'lib/taskmeister/task.rb', line 5

def notes
  @notes
end

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/taskmeister/task.rb', line 5

def text
  @text
end

Class Method Details

.create(text) ⇒ Object



26
27
28
# File 'lib/taskmeister/task.rb', line 26

def self.create(text)
  self.new(text, SecureRandom.uuid, "")
end

.from_markdown(lines) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/taskmeister/task.rb', line 30

def self.from_markdown(lines)
  task, *notes = *lines

  text, id = task_attributes(task)

  notes = notes.map { |l| l.gsub(/\A> ?/, "") }.join("\n")

  self.new(text, id, notes)
end

.task_attributes(line) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/taskmeister/task.rb', line 40

def self.task_attributes(line)
  matches = line.match(/\A(.+) - \[id\]\(([\w-]+)\)\z/)

  fail "Invalid task: #{line}" unless matches

  [matches[1], matches[2]]
end

Instance Method Details

#notes?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/taskmeister/task.rb', line 11

def notes?
  notes && !notes.empty?
end

#to_markdownObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/taskmeister/task.rb', line 15

def to_markdown
  [ "#{text} - [id](#{id})" ].tap do |a|
    return a unless notes.match(/\S/)
    a << ""
    a.concat notes.split("\n").map { |n|
      n.size > 0 ? "> #{n}" : ">"
    }
    a << ""
  end
end