Class: NattyUI::Table::ColumnCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/natty-ui/helper/table.rb

Overview

Collection of Column objects for a NattyUI::Table.

Accessed via #columns. Columns are created on demand; accessing a column index that has no data still returns a valid Column object whose cells reference the underlying table rows.

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ Column?

Returns the column at index, or nil if the index is beyond the table width.

Parameters:

  • index (#to_int)

    zero-based column index

Returns:



1191
1192
1193
1194
# File 'lib/natty-ui/helper/table.rb', line 1191

def [](index)
  index = index.to_int
  @columns[index] if index < max
end

#add(*texts, **attributes) {|column| ... } ⇒ Object, Column

Appends a new column (adds cells to each existing row) and returns it.

Examples:

Append and return the column

columns.add 'Alice', 'Bob', 'Carol', align: :left

Append and configure via block

columns.add('X', 'Y') { |col| col.attributes.align = :center }

Parameters:

  • texts (#to_s, ...)

    one text value per row in the new column

  • attributes (Hash)

    attribute options applied to the column, (see Cell#initialize)

Yields:

Yield Parameters:

Returns:



1211
1212
1213
1214
1215
1216
# File 'lib/natty-ui/helper/table.rb', line 1211

def add(*texts, **attributes)
  col = @columns[max]
  col.attributes.assign(attributes) unless attributes.empty?
  col.fill_text(0, *texts)
  block_given? ? yield(col) : col
end

#at(index) ⇒ Column?

Returns the column at index, or nil if no cells exist at that index.

Parameters:

  • index (#to_int)

    zero-based column index

Returns:



1182
1183
1184
# File 'lib/natty-ui/helper/table.rb', line 1182

def at(index)
  @columns.key?(index = index.to_int) and @columns[index]
end

#each {|column| ... } ⇒ nil, Enumerator

Iterates over columns that contain at least one non-nil cell.

Examples:

columns.each { |col| col.attributes.align = :center }

Yields:

Yield Parameters:

Returns:

  • (nil)

    if block is specified

  • (Enumerator)

    if no block is specified



1227
1228
1229
1230
1231
1232
1233
# File 'lib/natty-ui/helper/table.rb', line 1227

def each
  return to_enum unless block_given?
  max.times do |index|
    @table.rows.any? { it.at(index) } or next
    yield(@columns[index])
  end
end