Class: NattyUI::Table::Row

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

Instance Method Summary collapse

Instance Attribute Details

#any?Boolean (readonly)

Wheter the row contains at least one Cell.

Returns:

  • (Boolean)


# File 'lib/natty-ui/helper/table.rb', line 609

#attributesCell::Attributes (readonly)

Default formatting attributes for all cells in this row.

Returns:



620
621
622
# File 'lib/natty-ui/helper/table.rb', line 620

def attributes
  @attributes
end

#empty?Boolean (readonly)

Wheter the row is empty.

Returns:

  • (Boolean)


# File 'lib/natty-ui/helper/table.rb', line 605

#none?Boolean (readonly)

Wheter the row contains at no Cell.

Returns:

  • (Boolean)


# File 'lib/natty-ui/helper/table.rb', line 613

#sizeInteger (readonly)

Number of columns.

Returns:

  • (Integer)


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

Parameters:

  • index (#to_int)

    zero-based column index

Returns:



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.

Examples:

row[1] = 'Hello', align: :center

Parameters:

  • index (#to_int, #each)

    a single index or an enumerable of indices



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.

Examples:

Append and return the new cell

row.add 'Alice', align: :left

Append and configure via block

row.add 'Alice' do |cell|
  cell.attributes.align = :center
end

Parameters:

Yields:

  • (cell)

    the new Cell

Yield Parameters:

Returns:

  • (Object)

    return value of the block

  • (Cell)

    the new Cell, if no block is specified



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.

Examples:

Append cells and return them

row.add_text 'A', 'B', 'C', align: :center

Append cells and configure via block

row.add_text 'X', 'Y' do |cells|
  cells.each { it.attributes.align = :right }
end

Parameters:

Yields:

  • (cells)

    array of the new Cell objects

Yield Parameters:

  • cells (Array<Cell>)

Returns:

  • (Object)

    return value of the block

  • (Array<Cell>)

    array of the new Cell objects, if no block is specified



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.

Parameters:

  • index (#to_int)

    zero-based column index

Returns:



# File 'lib/natty-ui/helper/table.rb', line 622

#delete(index) ⇒ Boolean

Removes a cell from this row.

Parameters:

  • index (#to_int, Cell)

    column index or the Cell object itself

Returns:

  • (Boolean)

    true if a cell was removed, false otherwise



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.

Examples:

With a block

row.each { |cell| cell.attributes.align = :center }

Yields:

  • (cell)

    each non-nil Cell

Yield Parameters:

Returns:

  • (nil)

    if block is specified

  • (Enumerator)

    if no block is specified



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.

Examples:

Single index

row.fill 2, 'value'

Multiple indices (same content)

row.fill [0, 2, 4], 'x', align: :center

Configure via block

row.fill(1, 'val') { |cell| cell.attributes.vertical = :middle }

Parameters:

  • index (#to_int, #each)

    a single index or an enumerable of indices

  • text (#to_s, ...)

    cell text

  • attributes

    cell attributes (see Cell::Attributes#initialize)

Yields:

  • (cells)

    array of the new Cell objects

Yield Parameters:

  • cells (Array<Cell>)

Returns:

  • (Object)

    return value of the block

  • (Array<Cell>)

    array of the new Cell objects, if no block is specified



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.

Examples:

Fill and return the cells

row.fill_text 1, 'B', 'C', 'D'

Fill and configure via block

row.fill_text(1, 'B', 'C') do |cells|
  cells.each { it.attributes.align = :right }
end

Parameters:

  • index (#to_int)

    starting column index

  • texts (#to_s, ...)

    one text value per cell

  • attributes

    cell attributes (see Cell::Attributes#initialize)

Yields:

  • (cells)

    array of the new Cell objects

Yield Parameters:

  • cells (Array<Cell>)

Returns:

  • (Object)

    return value of the block

  • (Array<Cell>)

    array of the new Cell objects, if no block is specified



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.

Examples:

row.fill_while do |i|
  break if i >= 3
  ["Col #{i}", { align: :center }]
end

Parameters:

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

    starting index

Yields:

  • (index)

    current column index

Yield Parameters:

  • index (Integer)

Yield Returns:



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.

Examples:

Insert and return the new cell

row.insert 0, 'Header', align: :center

Insert and configure via block

row.insert(0, 'Header') { |c| c.attributes.align = :center }

Parameters:

  • index (#to_int)

    position to insert at

  • text (#to_s, ...)

    cell text

  • attributes

    cell attributes (see Cell::Attributes#initialize)

Yields:

  • (cell)

    the new Cell

Yield Parameters:

Returns:

  • (Object)

    return value of the block

  • (Cell)

    the new Cell, if no block is specified



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