Class: TTY::Table::ColumnSet Private
- Inherits:
-
Object
- Object
- TTY::Table::ColumnSet
- 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
- #table ⇒ Object readonly private
Class Method Summary collapse
-
.assert_widths(column_widths, table_widths) ⇒ Object
Assert data integrity for column widths.
-
.widths_from(table, column_widths = nil) ⇒ Array[Integer]
Converts column widths to array format or infers default widths.
Instance Method Summary collapse
-
#extract_widths ⇒ Array
private
Calcualte maximum column widths.
-
#initialize(table) ⇒ ColumnSet
constructor
Initialize a ColumnSet.
-
#total_width ⇒ Integer
Calculate total table width.
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
#table ⇒ Object (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
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
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_widths ⇒ Array
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
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_width ⇒ Integer
Calculate total table width
27 28 29 |
# File 'lib/tty/table/column_set.rb', line 27 def total_width extract_widths.reduce(:+) end |