Class: TTY::Table::ColumnSet Private

Inherits:
Object
  • Object
show all
Includes:
Equatable
Defined in:
lib/tty/table/column_set.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 that represents table column properties.

Used by TTY::Table to manage column sizing.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ ColumnSet

Initialize a ColumnSet



18
19
20
# File 'lib/tty/table/column_set.rb', line 18

def initialize(table)
  @table = table
end

Instance Attribute Details

#tableObject (readonly)

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



13
14
15
# File 'lib/tty/table/column_set.rb', line 13

def table
  @table
end

Class Method Details

.assert_widths(column_widths, table_widths) ⇒ Object

Assert data integrity for column widths

Parameters:

  • column_widths (Array)
  • table_widths (Integer)

Raises:



52
53
54
55
56
57
58
59
60
61
# File 'lib/tty/table/column_set.rb', line 52

def self.assert_widths(column_widths, table_widths)
  if column_widths.empty?
    fail InvalidArgument, 'Value for :column_widths must be ' \
                           'a non-empty array'
  end
  if column_widths.size != table_widths
    fail InvalidArgument, 'Value for :column_widths must match ' \
                           'table column count'
  end
end

.widths_from(table, column_widths = nil) ⇒ Array[Integer]

Converts column widths to array format or infers default widths

Parameters:

  • table (TTY::Table)
  • column_widths (Array, Numeric, NilClass) (defaults to: nil)

Returns:

  • (Array[Integer])


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

def self.widths_from(table, column_widths = nil)
  case column_widths
  when Array
    assert_widths(column_widths, table.column_size)
    Array(column_widths).map(&:to_i)
  when Numeric
    Array.new(table.column_size, column_widths)
  when NilClass
    new(table).extract_widths
  else
    fail TypeError, 'Invalid type for column widths'
  end
end

Instance Method Details

#extract_widthsArray

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

Calcualte maximum column widths

Returns:

  • (Array)

    column widths



36
37
38
39
40
41
# File 'lib/tty/table/column_set.rb', line 36

def extract_widths
  data     = table.data
  colcount = data.max { |row_a, row_b| row_a.size <=> row_b.size }.size

  self.class.find_maximas(colcount, data)
end

#total_widthInteger

Calculate total table width

Returns:

  • (Integer)


27
28
29
# File 'lib/tty/table/column_set.rb', line 27

def total_width
  extract_widths.reduce(:+)
end