Class: NattyUI::Table::RowCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/natty-ui/helper/table.rb

Overview

Ordered collection of Row objects belonging to a NattyUI::Table. Accessed via #rows.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#any?Boolean (readonly)

Wheter the collection contains at least one NattyUI::Table::Row.

Returns:

  • (Boolean)


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

#empty?Boolean (readonly)

Wheter the collection is empty.

Returns:

  • (Boolean)


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

#none?Boolean (readonly)

Wheter the collection contains at no NattyUI::Table::Row.

Returns:

  • (Boolean)


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

#sizeInteger (readonly)

Number of rows.

Returns:

  • (Integer)


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

Instance Method Details

#[](index) ⇒ Row

Returns the row at index, creating an empty row if none exists.

Examples:

rows[0]   # => Row (created if missing)

Parameters:

  • index (#to_int)

    zero-based row index

Returns:



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

def [](index) = @rows[index.to_int] ||= Row.new

#add(*texts, **attributes) {|row| ... } ⇒ Object, Row

Appends a new row, optionally pre-populated with text cells.

Examples:

Append and return the new row

rows.add 'Alice', 'Bob', align: :center

Append and populate via block

rows.add do |row|
  row.add 'Name', align: :right
  row.add 'Score', align: :left
end

Parameters:

  • texts (#to_s, ...)

    text values; each becomes a cell in the new row

  • attributes (Hash)

    default attributes applied to the row, see Cell::Attributes#initialize

Yields:

Yield Parameters:

Returns:

  • (Object)

    return value of the block

  • (Row)

    the new row, if no block is specified



1052
1053
1054
1055
1056
# File 'lib/natty-ui/helper/table.rb', line 1052

def add(*texts, **attributes)
  @rows << (row = Row.new(**attributes))
  row.add_text(*texts) unless texts.empty?
  block_given? ? yield(row) : row
end

#add_text(*texts, **attributes) {|rows| ... } ⇒ Object, Array<Row>

Appends one new row per text value (each value becomes a single-cell row).

Examples:

Append and return the rows

rows.add_text 'Row A', 'Row B'

Append and configure via block

rows.add_text('Row A', 'Row B') { |rows| rows.each { it.attributes.align = :center } }

Parameters:

  • texts (#to_s, ...)

    one text value per row

  • attributes (Hash)

    default attributes applied to each row, see Cell::Attributes#initialize

Yields:

Yield Parameters:

  • rows (Array<Row>)

Returns:

  • (Object)

    return value of the block

  • (Array<Row>)

    the new rows, if no block is specified



1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'lib/natty-ui/helper/table.rb', line 1092

def add_text(*texts, **attributes)
  @rows.concat(
    rows =
      texts.map! do |txt|
        row = Row.new(**attributes)
        row.add(*txt)
        row
      end
  )
  block_given? ? yield(rows) : rows
end

#at(index) ⇒ Row?

Returns the row at index, or nil if it does not exist.

Examples:

rows.at(0)   # => first Row or nil

Parameters:

  • index (#to_int)

    zero-based row index

Returns:



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

def at(index) = @rows[index]

#delete(index) ⇒ Boolean

Removes a row from the collection.

Parameters:

Returns:

  • (Boolean)

    true if a row was removed, false otherwise



1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/natty-ui/helper/table.rb', line 1108

def delete(index)
  row =
    if Row === index
      @rows.delete(index)
    else
      @rows.delete_at(index.to_int)
    end
  !row.freeze.nil?
end

#each {|row| ... } ⇒ nil, Enumerator

Iterates over all non-nil rows.

Examples:

rows.each { |r| r.attributes.align = :center }

Yields:

Yield Parameters:

Returns:

  • (nil)

    if block is specified

  • (Enumerator)

    if no block is specified



1127
1128
1129
1130
1131
# File 'lib/natty-ui/helper/table.rb', line 1127

def each
  return to_enum unless block_given?
  @rows.each { yield(it) if it }
  nil
end

#each_filled {|row| ... } ⇒ nil, Enumerator

Iterates over non-nil rows that contain at least one cell.

Yields:

Yield Parameters:

Returns:

  • (nil)

    if block is specified

  • (Enumerator)

    if no block is specified



1138
1139
1140
1141
1142
# File 'lib/natty-ui/helper/table.rb', line 1138

def each_filled
  return to_enum(__method__) unless block_given?
  @rows.each { yield(it) if it&.any? }
  nil
end

#insert(index, *texts, **attributes) {|row| ... } ⇒ Object, Row

Inserts a new row at the given index.

Examples:

Insert and return the new row

rows.insert 0, 'Header A', 'Header B', align: :center

Insert and populate via block

rows.insert(0) { |row| row.add 'Header' }

Parameters:

  • index (#to_int)

    position to insert at

  • texts (#to_s, ...)

    text values; each becomes a cell in the new row

  • attributes (Hash)

    default attributes applied to the row, see Cell::Attributes#initialize

Yields:

Yield Parameters:

Returns:

  • (Object)

    return value of the block

  • (Row)

    the new row, if no block is specified



1071
1072
1073
1074
1075
# File 'lib/natty-ui/helper/table.rb', line 1071

def insert(index, *texts, **attributes)
  @rows.insert(index.to_int, row = Row.new(**attributes))
  row.add_text(*texts) unless texts.empty?
  block_given? ? yield(row) : row
end