Module: Todown

Defined in:
lib/todown.rb,
lib/todown/version.rb

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.task_to_hash_table(tasks) ⇒ Hash<Symbol, Array>

Manipulate data to transform in the expected format for ‘Terminal::Table`

Parameters:

  • tasks (Array<Task>)

Returns:

  • (Hash<Symbol, Array>)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/todown.rb', line 20

def self.task_to_hash_table tasks
  rows = []
  attributes_keys = []

  tasks.each do |task|
    task.attributes.keys.each {|key| attributes_keys << key.to_s }
  end

  attributes_keys.uniq!

  headings = ['', 'Name'].concat attributes_keys

  tasks.each do |task|
    row = []
    row << (task.finished ? 'X' : '')
    row << task.name

    # Insert attributes in the same order
    attributes_keys.each do |attribute_key|
      attribute_value = task.attributes[attribute_key.to_sym]
      if attribute_value
        row << attribute_value.to_s
      else
        row << ''
      end
    end

    rows << row
  end

  return {rows: rows, headings: headings }
end

.task_to_table(tasks) ⇒ Terminal::Table

Create a ‘Terminal::Table` from many task

Parameters:

  • tasks (Array<Task>)

Returns:

  • (Terminal::Table)


12
13
14
# File 'lib/todown.rb', line 12

def self.task_to_table tasks
  return Terminal::Table.new(Todown.task_to_hash_table(tasks))
end

.tasks_from_file(filepath) {|Task| ... } ⇒ Array<Task>

Create many task from given filepath

Parameters:

  • filepath (String)

    filepath of readable markdown file

Yields:

Returns:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/todown.rb', line 59

def self.tasks_from_file filepath
  tasks = []

  File.read(filepath).scan(/- \[( |X|x)\] (.+)/).each do |data|
    task = Task.new data[1], (data[0] != ' ')
    tasks << task
    yield task if block_given?
  end

  return tasks
end

.tasks_from_path(path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/todown.rb', line 72

def self.tasks_from_path path
  tasks = []

  Find.find(path).select{|f| File.file?(f) and File.readable?(f)}.each do |file|
    self.tasks_from_file(file) do |task|
      tasks << task
      yield task
    end
  end

  return tasks
end