Class: NattyUI::Table
- Inherits:
-
Object
- Object
- NattyUI::Table
- Defined in:
- lib/natty-ui/helper/table.rb
Overview
Data structure for building terminal tables.
A Table is populated through its #rows and #columns collections and
then passed to Features#table for rendering. Cells accept text and
formatting attributes; rows and columns can carry default attributes that
are merged with individual cell attributes during rendering.
Defined Under Namespace
Classes: Cell, Column, ColumnCollection, Row, RowCollection
Instance Attribute Summary collapse
-
#columns ⇒ ColumnCollection
readonly
Column collection for this table.
-
#empty? ⇒ Boolean
readonly
Returns
truewhen the table has no cells. -
#rows ⇒ RowCollection
readonly
Row collection for this table.
Instance Method Summary collapse
-
#[](row_index, column_index = nil) ⇒ Row, Cell
Returns (or creates) the row at
row_index, or the cell at[row_index, column_index]. -
#add_column(*texts, **attributes) {|column| ... } ⇒ Object, Column
Appends a new column to the table.
-
#add_row(*texts, **attributes) {|row| ... } ⇒ Object, Row
Appends a new row to the table.
-
#at(row_index, column_index = nil) ⇒ Row, ...
Returns the row at
row_index, or the cell at[row_index, column_index]. -
#each_column {|column| ... } ⇒ nil, Enumerator
Iterates over all columns in the table.
-
#each_row {|row| ... } ⇒ nil, Enumerator
Iterates over all rows in the table.
Instance Attribute Details
#columns ⇒ ColumnCollection (readonly)
Column collection for this table.
1256 1257 1258 |
# File 'lib/natty-ui/helper/table.rb', line 1256 def columns @columns end |
#empty? ⇒ Boolean (readonly)
Returns true when the table has no cells.
1262 |
# File 'lib/natty-ui/helper/table.rb', line 1262 def empty? = @rows.none? |
#rows ⇒ RowCollection (readonly)
Row collection for this table.
1251 1252 1253 |
# File 'lib/natty-ui/helper/table.rb', line 1251 def rows @rows end |
Instance Method Details
#[](row_index, column_index = nil) ⇒ Row, Cell
Returns (or creates) the row at row_index, or the cell at
[row_index, column_index].
1294 1295 1296 1297 |
# File 'lib/natty-ui/helper/table.rb', line 1294 def [](row_index, column_index = nil) row = @rows[row_index] column_index ? row[column_index] : row end |
#add_column(*texts, **attributes) {|column| ... } ⇒ Object, Column
Appends a new column to the table.
1321 1322 1323 |
# File 'lib/natty-ui/helper/table.rb', line 1321 def add_column(*texts, **attributes, &) @columns.add(*texts, **attributes, &) end |
#add_row(*texts, **attributes) {|row| ... } ⇒ Object, Row
Appends a new row to the table.
1308 1309 1310 |
# File 'lib/natty-ui/helper/table.rb', line 1308 def add_row(*texts, **attributes, &) @rows.add(*texts, **attributes, &) end |
#at(row_index, column_index = nil) ⇒ Row, ...
Returns the row at row_index, or the cell at [row_index, column_index].
Returns nil when the row (or cell) does not exist.
1277 1278 1279 1280 |
# File 'lib/natty-ui/helper/table.rb', line 1277 def at(row_index, column_index = nil) row = @rows.at(row_index) or return column_index ? row.at(column_index) : row end |
#each_column {|column| ... } ⇒ nil, Enumerator
Iterates over all columns in the table.
1343 |
# File 'lib/natty-ui/helper/table.rb', line 1343 def each_column(&) = @columns.each(&) |
#each_row {|row| ... } ⇒ nil, Enumerator
Iterates over all rows in the table.
1333 |
# File 'lib/natty-ui/helper/table.rb', line 1333 def each_row(&) = @rows.each(&) |