Class: Taql::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/taql/table.rb

Constant Summary collapse

DASH =
"-".freeze
PLUS =
"+".freeze
SPACE =
" ".freeze
VERTICAL_BAR =
"|".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries, markdown: false) ⇒ Table

Returns a new instance of Table.



10
11
12
13
# File 'lib/taql/table.rb', line 10

def initialize(entries, markdown: false)
  @entries = entries.map { |entry| entry.transform_values(&:to_s) }
  @markdown = markdown
end

Instance Attribute Details

#markdownObject

Returns the value of attribute markdown.



8
9
10
# File 'lib/taql/table.rb', line 8

def markdown
  @markdown
end

Instance Method Details

#bodyObject



15
16
17
# File 'lib/taql/table.rb', line 15

def body
  entries.map(&:values)
end

#columnsObject



19
20
21
22
23
# File 'lib/taql/table.rb', line 19

def columns
  headers.map do |header|
    [header, *entries.map { |entry| entry[header] }]
  end
end

#headersObject



25
26
27
# File 'lib/taql/table.rb', line 25

def headers
  entries.map(&:keys).uniq.flatten
end


29
30
31
# File 'lib/taql/table.rb', line 29

def print
  output if entries.any?
end

#rowsObject



33
34
35
# File 'lib/taql/table.rb', line 33

def rows
  [headers, *body]
end