Class: CommandKit::Printing::Tables::TableBuilder Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/command_kit/printing/tables/table_builder.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.

Builds a table and calculates it's dimensions.

Since:

  • 0.4.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows = [], header: nil) ⇒ TableBuilder

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.

Initializes the table.

Parameters:

  • rows (Array<Array>) (defaults to: [])

    The rows for the table.

  • header (Array) (defaults to: nil)

    The header row.

Since:

  • 0.4.0



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/command_kit/printing/tables/table_builder.rb', line 61

def initialize(rows=[], header: nil)
  @rows    = []
  @height  = 0
  @width   = 0
  @column_widths = []

  @max_rows    = 0
  @max_columns = 0

  @header = !header.nil?

  self << header if header
  rows.each { |row| self << row }
end

Instance Attribute Details

#column_widthsArray<Integer> (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.

The widths of the columns in the table.

Returns:

  • (Array<Integer>)

Since:

  • 0.4.0



50
51
52
# File 'lib/command_kit/printing/tables/table_builder.rb', line 50

def column_widths
  @column_widths
end

#headerBoolean (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.

Indicates whether the table has a header row.

Returns:

  • (Boolean)

Since:

  • 0.4.0



25
26
27
# File 'lib/command_kit/printing/tables/table_builder.rb', line 25

def header
  @header
end

#heightInteger (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.

The height (in lines) of the table.

Returns:

  • (Integer)

Since:

  • 0.4.0



30
31
32
# File 'lib/command_kit/printing/tables/table_builder.rb', line 30

def height
  @height
end

#max_columnsInteger (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.

The maximum number of columns in the table.

Returns:

  • (Integer)

Since:

  • 0.4.0



45
46
47
# File 'lib/command_kit/printing/tables/table_builder.rb', line 45

def max_columns
  @max_columns
end

#max_rowsInteger (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.

The maximum number of rows in the table.

Returns:

  • (Integer)

Since:

  • 0.4.0



40
41
42
# File 'lib/command_kit/printing/tables/table_builder.rb', line 40

def max_rows
  @max_rows
end

#rowsArray<RowBuilder> (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.

The rows within the table.

Returns:

Since:

  • 0.4.0



20
21
22
# File 'lib/command_kit/printing/tables/table_builder.rb', line 20

def rows
  @rows
end

#widthInteger (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.

The width (in characters) of the table.

Returns:

  • (Integer)

Since:

  • 0.4.0



35
36
37
# File 'lib/command_kit/printing/tables/table_builder.rb', line 35

def width
  @width
end

Instance Method Details

#<<(row) ⇒ self

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.

Appends a row to the table.

Parameters:

  • row (Array)

    The row to append.

Returns:

  • (self)

Since:

  • 0.4.0



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/command_kit/printing/tables/table_builder.rb', line 94

def <<(row)
  new_row = RowBuilder.new(row)

  new_row.each_with_index do |cell,index|
    column_width = @column_widths[index] || 0

    if cell.width > column_width
      @column_widths[index] = cell.width
    end
  end

  @height += new_row.height
  @width   = new_row.width if new_row.width > @width

  @max_rows   += 1
  @max_columns = new_row.columns if new_row.columns > @max_columns

  @rows << new_row
  return self
end

#[](row_index) ⇒ RowBuilder?

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.

Fetches a row from the table.

Parameters:

  • row_index (Integer)

    The row index to fetch the row at.

Returns:

  • (RowBuilder, nil)

    The row or nil if no row exists at the given row index.

Since:

  • 0.4.0



124
125
126
# File 'lib/command_kit/printing/tables/table_builder.rb', line 124

def [](row_index)
  @rows[row_index]
end

#each {|row| ... } ⇒ Enumerator

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.

Enumerates over each row in the table.

Yields:

  • (row)

    If a block is given, it will be passed each row within the table.

Yield Parameters:

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Since:

  • 0.4.0



140
141
142
# File 'lib/command_kit/printing/tables/table_builder.rb', line 140

def each(&block)
  @rows.each(&block)
end

#header?Boolean

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.

Determines whether the table has a header row.

Returns:

  • (Boolean)

    Indicates whether the first row of the table is a header row.

Since:

  • 0.4.0



82
83
84
# File 'lib/command_kit/printing/tables/table_builder.rb', line 82

def header?
  @header
end