Class: NattyUI::Table::Column

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#attributesCell::Attributes (readonly)

Default formatting attributes for all cells in this column.

Returns:



842
843
844
# File 'lib/natty-ui/helper/table.rb', line 842

def attributes
  @attributes
end

#indexInteger (readonly)

Zero-based index of this column in the table.

Returns:

  • (Integer)


837
838
839
# File 'lib/natty-ui/helper/table.rb', line 837

def index
  @index
end

#sizeInteger (readonly)

Number of rows in the owning table.

Returns:

  • (Integer)


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.

Parameters:

  • index (#to_int)

    zero-based row index

Returns:



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.

Examples:

col[0] = 'Header', align: :center

Parameters:



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.

Parameters:

  • index (#to_int)

    zero-based row index

Returns:



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).

Returns:

  • (Integer, nil)

    number of cells deleted, or nil if the column was already empty



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.

Examples:

col.each { |cell| cell.attributes.align = :right }

Yields:

Yield Parameters:

Returns:

  • (nil)

    if block is specified

  • (Enumerator)

    if no block is specified



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.

Examples:

Single index

col.fill 0, 'Header', align: :center

Multiple indices and configure via block

col.fill([0, 1], 'x') { |cells| cells.each { it.attributes.align = :right } }

Parameters:

Yields:

  • (result)

    cell or array of cells

Returns:

  • (Object)

    return value of the block

  • (Cell, Array<Cell>)

    cell or array of cells, if no block is specified



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.

Examples:

Fill and return the cells

col.fill_text 0, 'A', 'B', 'C'

Fill and configure via block

col.fill_text(0, 'A', 'B') { |cells| cells.each { it.attributes.align = :center } }

Parameters:

Yields:

  • (cells)

    array of created cells

Returns:

  • (Object)

    return value of the block

  • (Array<Cell>)

    array of created cells, if no block is specified



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.

Examples:

col.fill_while { |i| i < 3 ? ["Row #{i}"] : nil }

Parameters:

  • index (#to_int) (defaults to: 0)

    starting row index

Yields:

  • (index)

    current row index

Yield Parameters:

  • index (Integer)

Yield Returns:



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