Module: Todown

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

Constant Summary collapse

VERSION =
"0.2.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>)


19
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
# File 'lib/todown.rb', line 19

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)


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

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