Class: NattyUI::Table

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

Overview

TODO:

This chapter needs more documentation.

Collection of rows and columns used by Features#table.

Defined Under Namespace

Classes: Attributes, Cell, Column, ColumnsCollection, Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnsColumnsCollection (readonly)

Returns table columns.

Returns:



230
231
232
# File 'lib/natty-ui/table.rb', line 230

def columns
  @columns
end

Instance Method Details

#[](row_index, column_index = nil) ⇒ Object



242
243
244
245
# File 'lib/natty-ui/table.rb', line 242

def [](row_index, column_index = nil)
  row = @rows[row_index] or return
  column_index ? row[column_index] : row
end

#add(*text, **attributes) ⇒ Row

Returns created row.

Returns:

  • (Row)

    created row



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/natty-ui/table.rb', line 248

def add(*text, **attributes)
  unless text[0].is_a?(Hash)
    @rows << (nr = Row.new)
    text.each { nr.add(_1, **attributes) }
    return block_given? ? yield(nr) : nr
  end
  new_rows = []
  text[0].each_pair do |key, value|
    new_rows << (nr = Row.new)
    @rows << nr
    nr.add(key, **attributes)
    nr.add(value, **attributes)
    yield(nr) if block_given?
  end
  new_rows
end

#column_countInteger

Returns count of columns.

Returns:

  • (Integer)

    count of columns



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

def column_count = @rows.empty? ? 0 : @rows.max_by(&:count).count

#countInteger

Returns count of rows.

Returns:

  • (Integer)

    count of rows



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

def count = @rows.size

#delete(row) ⇒ Object



265
266
267
# File 'lib/natty-ui/table.rb', line 265

def delete(row)
  row.is_a?(Row) ? @rows.delete(row) : @rows.delete_at(row)
end

#each(&block) ⇒ Object



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

def each(&block) = @rows.each(&block)

#each_cell_of(column_index) ⇒ Object



269
270
271
272
273
274
# File 'lib/natty-ui/table.rb', line 269

def each_cell_of(column_index)
  return to_enum(__method__, column_index) unless block_given?
  return if (column_index = column_index.to_i) >= column_count
  @rows.each { yield(_1[column_index]) }
  nil
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty? = @rows.empty?