Module: Philiprehberger::Table

Defined in:
lib/philiprehberger/table.rb,
lib/philiprehberger/table/styles.rb,
lib/philiprehberger/table/version.rb,
lib/philiprehberger/table/renderer.rb

Defined Under Namespace

Modules: Styles Classes: Grid, Renderer

Constant Summary collapse

ANSI_PATTERN =
/\e\[[0-9;]*m/
VERSION =
'0.5.0'

Class Method Summary collapse

Class Method Details

.from_csv(string, align: {}, max_width: {}) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/philiprehberger/table.rb', line 27

def self.from_csv(string, align: {}, max_width: {})
  parsed = CSV.parse(string)
  return Grid.new(headers: [], rows: [], align: align, max_width: max_width) if parsed.empty?

  headers = parsed.first.map { |h| h.nil? ? '' : h.to_s }
  rows = parsed.drop(1).map { |row| row.map { |cell| cell.nil? ? '' : cell.to_s } }
  Grid.new(headers: headers, rows: rows, align: align, max_width: max_width)
end

.from_hashes(data, align: {}, max_width: {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/philiprehberger/table.rb', line 16

def self.from_hashes(data, align: {}, max_width: {})
  return Grid.new(headers: [], rows: [], align: align, max_width: max_width) if data.empty?

  headers = data.each_with_object([]) do |hash, keys|
    hash.each_key { |k| keys << k unless keys.include?(k) }
  end

  rows = data.map { |hash| headers.map { |h| hash[h] } }
  Grid.new(headers: headers.map(&:to_s), rows: rows, align: align, max_width: max_width)
end

.new(headers:, rows: [], align: {}, max_width: {}) ⇒ Object



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

def self.new(headers:, rows: [], align: {}, max_width: {})
  Grid.new(headers: headers, rows: rows, align: align, max_width: max_width)
end