Class: NattyUI::Table::Row
- Inherits:
-
Object
- Object
- NattyUI::Table::Row
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/natty-ui/helper/table.rb
Overview
A single row in a NattyUI::Table.
Rows are accessed through #rows or created implicitly by #add_row and #[]. A row holds an ordered list of Cell objects and carries default formatting #attributes that apply to all cells in the row during rendering.
Instance Attribute Summary collapse
-
#any? ⇒ Boolean
readonly
Wheter the row contains at least one Cell.
-
#attributes ⇒ Cell::Attributes
readonly
Default formatting attributes for all cells in this row.
-
#empty? ⇒ Boolean
readonly
Wheter the row is empty.
-
#none? ⇒ Boolean
readonly
Wheter the row contains at no Cell.
-
#size ⇒ Integer
readonly
Number of columns.
Instance Method Summary collapse
-
#[](index) ⇒ Cell
Returns the cell at
index, creating an empty cell if none exists. -
#[]=(index, *args) ⇒ Object
Sets or replaces the cell at
index. -
#add(*text, **attributes) {|cell| ... } ⇒ Object, Cell
Appends a new cell to this row.
-
#add_text(*texts, **attributes) {|cells| ... } ⇒ Object, Array<Cell>
Appends one cell per text value.
-
#at(index) ⇒ Cell?
Returns the Cell at
index, ornilif it does not exist. -
#delete(index) ⇒ Boolean
Removes a cell from this row.
-
#each {|cell| ... } ⇒ nil, Enumerator
Iterates over non-nil cells in this row.
-
#fill(index, *text, **attributes) {|cells| ... } ⇒ Object, Array<Cell>
Sets the cell at one or more indices.
-
#fill_text(index, *texts, **attributes) {|cells| ... } ⇒ Object, Array<Cell>
Fills consecutive cells starting at
indexwith one cell per text value. -
#fill_while(index = 0) {|index| ... } ⇒ Object
Fills cells sequentially by calling the block for each index.
-
#insert(index, *texts, **attributes) {|cell| ... } ⇒ Object, Cell
Inserts a new cell at the given index.
Instance Attribute Details
#any? ⇒ Boolean (readonly)
Wheter the row contains at least one Cell.
|
|
# File 'lib/natty-ui/helper/table.rb', line 609
|
#attributes ⇒ Cell::Attributes (readonly)
Default formatting attributes for all cells in this row.
620 621 622 |
# File 'lib/natty-ui/helper/table.rb', line 620 def attributes @attributes end |
#empty? ⇒ Boolean (readonly)
Wheter the row is empty.
|
|
# File 'lib/natty-ui/helper/table.rb', line 605
|
#none? ⇒ Boolean (readonly)
Wheter the row contains at no Cell.
|
|
# File 'lib/natty-ui/helper/table.rb', line 613
|
#size ⇒ Integer (readonly)
Number of columns.
|
|
# File 'lib/natty-ui/helper/table.rb', line 601
|
Instance Method Details
#[](index) ⇒ Cell
Returns the cell at index, creating an empty cell if none exists.
631 |
# File 'lib/natty-ui/helper/table.rb', line 631 def [](index) = @cells[index.to_int] ||= Cell.new |
#[]=(index, *args) ⇒ Object
Sets or replaces the cell at index.
Accepts the same arguments as Cell#initialize: optional text values followed by an optional keyword hash of Cell::Attributes options.
642 643 644 645 |
# File 'lib/natty-ui/helper/table.rb', line 642 def []=(index, *args) opts = args.pop if args[-1].is_a?(Hash) fill(index, *args, **opts) end |
#add(*text, **attributes) {|cell| ... } ⇒ Object, Cell
Appends a new cell to this row.
662 663 664 665 |
# File 'lib/natty-ui/helper/table.rb', line 662 def add(*text, **attributes) @cells << (cell = Cell.new(*text, **attributes)) block_given? ? yield(cell) : cell end |
#add_text(*texts, **attributes) {|cells| ... } ⇒ Object, Array<Cell>
Appends one cell per text value.
702 703 704 705 706 |
# File 'lib/natty-ui/helper/table.rb', line 702 def add_text(*texts, **attributes) cells = texts.map! { Cell.new(it, **attributes) } @cells.concat(cells) block_given? ? yield(cells) : cells end |
#at(index) ⇒ Cell?
Returns the Cell at index, or nil if it does not exist.
|
|
# File 'lib/natty-ui/helper/table.rb', line 622
|
#delete(index) ⇒ Boolean
Removes a cell from this row.
786 787 788 789 790 791 792 793 794 |
# File 'lib/natty-ui/helper/table.rb', line 786 def delete(index) cell = if Cell === index @cells.delete(index) else @cells.delete_at(index.to_int) end !cell.freeze.nil? end |
#each {|cell| ... } ⇒ nil, Enumerator
Iterates over non-nil cells in this row.
805 806 807 808 809 |
# File 'lib/natty-ui/helper/table.rb', line 805 def each return to_enum unless block_given? @cells.each { yield(it) if it } nil end |
#fill(index, *text, **attributes) {|cells| ... } ⇒ Object, Array<Cell>
Sets the cell at one or more indices.
724 725 726 727 728 729 730 731 732 |
# File 'lib/natty-ui/helper/table.rb', line 724 def fill(index, *text, **attributes) ret = if index.respond_to?(:each) index.each.map { @cells[it] = Cell.new(*text, **attributes) } else @cells[index] = Cell.new(*text, **attributes) end block_given? ? yield(ret) : ret end |
#fill_text(index, *texts, **attributes) {|cells| ... } ⇒ Object, Array<Cell>
Fills consecutive cells starting at index with one cell per text value.
776 777 778 779 780 |
# File 'lib/natty-ui/helper/table.rb', line 776 def fill_text(index, *texts, **attributes) index = index.to_int - 1 cells = texts.map! { @cells[index += 1] = Cell.new(it, **attributes) } block_given? ? yield(cells) : cells end |
#fill_while(index = 0) {|index| ... } ⇒ Object
Fills cells sequentially by calling the block for each index.
The block receives the current Integer index and must return an
Array whose elements are the text/attribute arguments for the new
cell (an optional trailing Hash is treated as attribute options).
Returning nil or false stops iteration.
752 753 754 755 756 757 758 759 |
# File 'lib/natty-ui/helper/table.rb', line 752 def fill_while(index = 0) index = index.to_int while (args = yield(index)) opts = args.pop if args[-1].is_a?(Hash) @cells[index] = Cell.new(*args, **opts) index += 1 end end |
#insert(index, *texts, **attributes) {|cell| ... } ⇒ Object, Cell
Inserts a new cell at the given index.
680 681 682 683 |
# File 'lib/natty-ui/helper/table.rb', line 680 def insert(index, *texts, **attributes) @cells.insert(index.to_int, cell = Cell.new(*texts, **attributes)) block_given? ? yield(cell) : cell end |