Class: TTY::Table::Transformation Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A class for transforming table values

Used internally by TTY::Table

Class Method Summary collapse

Class Method Details

.extract_tuples(args) ⇒ Object

Extract the header and row tuples from the value

Parameters:

  • args (Array)

Returns:

  • (Object)


18
19
20
21
22
23
24
25
# File 'lib/tty/table/transformation.rb', line 18

def self.extract_tuples(args)
  rows   = args.pop
  header = args.size.zero? ? nil : args.first
  if rows.first.is_a?(Hash)
    header, rows = group_header_and_rows(rows)
  end
  { header: header, rows: rows }
end

.group_header_and_rows(value) ⇒ Object

Group hash keys into header and values into rows

Parameters:

  • value (Hash)


32
33
34
35
36
# File 'lib/tty/table/transformation.rb', line 32

def self.group_header_and_rows(value)
  header = value.map(&:keys).flatten.uniq
  rows   = value.reduce([]) { |arr, el| arr + el.values }
  [header, rows]
end