Class: Taskmeister::TaskListReader

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

Class Method Summary collapse

Class Method Details

.from_markdown(file_lines, file_path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/taskmeister/task_list_reader.rb', line 5

def self.from_markdown(file_lines, file_path)
  lines_grouped_by_task = \
    file_lines.map(&:chomp)
              .reduce([]) do |acc, l|
                acc.tap do |a|
                  if l.match(/#\s.+\s\[∞\]\(#[\w-]+\)/)
                    acc << [l]    # A new task
                  else
                    acc.last << l # A line of note for the latest task
                  end
                end
              end

  tasks = lines_grouped_by_task.map do |ls|
    Task.from_markdown ls
  end

  TaskList.new tasks, file_path
end

.from_markdown_file(file_path) ⇒ Object



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

def self.from_markdown_file(file_path)
  lines = File.exist?(file_path) ? File.readlines(file_path) : []
  self.from_markdown lines, file_path
end