Class: NattyUI::Table::Column
- Inherits:
-
Object
- Object
- NattyUI::Table::Column
- Includes:
- Enumerable
- Defined in:
- lib/natty-ui/helper/table.rb
Overview
A vertical slice through a NattyUI::Table — all cells at a given column index.
Columns are accessed through #columns. A Column holds a
reference to its owning NattyUI::Table and provides a view of cells at its
#index. The #attributes object carries default formatting applied to
all cells in the column during rendering.
Instance Attribute Summary collapse
-
#attributes ⇒ Cell::Attributes
readonly
Default formatting attributes for all cells in this column.
-
#index ⇒ Integer
readonly
Zero-based index of this column in the table.
-
#size ⇒ Integer
readonly
Number of rows in the owning table.
Instance Method Summary collapse
-
#[](index) ⇒ Cell
Returns the cell at row
index, creating an empty cell if needed. -
#[]=(index, *args) ⇒ Object
Sets or replaces the cell at
index. -
#at(index) ⇒ Cell?
Returns the cell at row
index, ornilif it does not exist. -
#delete! ⇒ Integer?
Removes this column from the table (deletes all cells at this index).
-
#each {|cell| ... } ⇒ nil, Enumerator
Iterates over non-nil cells in this column.
-
#fill(index, *text, **attributes) {|result| ... } ⇒ Object, ...
Sets the cell at one or more row indices.
-
#fill_text(index, *texts, **attributes) {|cells| ... } ⇒ Object, Array<Cell>
Fills consecutive rows starting at
indexwith one cell per text value. -
#fill_while(index = 0) {|index| ... } ⇒ Object
Fills cells in this column sequentially by calling the block for each row.
Instance Attribute Details
#attributes ⇒ Cell::Attributes (readonly)
Default formatting attributes for all cells in this column.
842 843 844 |
# File 'lib/natty-ui/helper/table.rb', line 842 def attributes @attributes end |
#index ⇒ Integer (readonly)
Zero-based index of this column in the table.
837 838 839 |
# File 'lib/natty-ui/helper/table.rb', line 837 def index @index end |
#size ⇒ Integer (readonly)
Number of rows in the owning table.
848 |
# File 'lib/natty-ui/helper/table.rb', line 848 def size = @table.rows.size |
Instance Method Details
#[](index) ⇒ Cell
Returns the cell at row index, creating an empty cell if needed.
860 |
# File 'lib/natty-ui/helper/table.rb', line 860 def [](index) = @table[index, @index] |
#[]=(index, *args) ⇒ Object
Sets or replaces the cell at index.
Accepts the same arguments as NattyUI::Table::Cell#initialize: optional text values followed by an optional keyword hash of NattyUI::Table::Cell::Attributes options.
872 873 874 875 |
# File 'lib/natty-ui/helper/table.rb', line 872 def []=(index, *args) opts = args.pop if args[-1].is_a?(Hash) fill(index, *args, **opts) end |
#at(index) ⇒ Cell?
Returns the cell at row index, or nil if it does not exist.
854 |
# File 'lib/natty-ui/helper/table.rb', line 854 def at(index) = @table.at(index, @index) |
#delete! ⇒ Integer?
Removes this column from the table (deletes all cells at this index).
950 951 952 |
# File 'lib/natty-ui/helper/table.rb', line 950 def delete! @table.rows.each.find_all { it.delete(@index) }.size.nonzero? end |
#each {|cell| ... } ⇒ nil, Enumerator
Iterates over non-nil cells in this column.
963 964 965 966 967 |
# File 'lib/natty-ui/helper/table.rb', line 963 def each return to_enum unless block_given? @table.rows.each { (cell = it.at(@index)) and yield(cell) } nil end |
#fill(index, *text, **attributes) {|result| ... } ⇒ Object, ...
Sets the cell at one or more row indices.
891 892 893 894 895 896 897 898 899 |
# File 'lib/natty-ui/helper/table.rb', line 891 def fill(index, *text, **attributes) ret = if index.respond_to?(:each) index.each.map { @table.rows[it].fill(@index, *text, **attributes) } else @table.rows[index].fill(@index, *text, **attributes) end block_given? ? yield(ret) : ret end |
#fill_text(index, *texts, **attributes) {|cells| ... } ⇒ Object, Array<Cell>
Fills consecutive rows starting at index with one cell per text value.
916 917 918 919 920 921 |
# File 'lib/natty-ui/helper/table.rb', line 916 def fill_text(index, *texts, **attributes) index = index.to_int - 1 rows = texts.map! { @table.rows[index += 1].fill(@index, it, **attributes) } block_given? ? yield(rows) : rows end |
#fill_while(index = 0) {|index| ... } ⇒ Object
Fills cells in this column sequentially by calling the block for each row.
The block receives the current Integer row index and must return an
Array of arguments for the cell (optional trailing Hash for
attributes), or nil / false to stop.
937 938 939 940 941 942 943 944 |
# File 'lib/natty-ui/helper/table.rb', line 937 def fill_while(index = 0) index = index.to_int while (args = yield(index)) opts = args.pop if args[-1].is_a?(Hash) @table.rows[index].fill(@index, *args, **opts) index += 1 end end |