Class: NattyUI::Table
- Inherits:
-
Object
- Object
- NattyUI::Table
- 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
-
#columns ⇒ ColumnsCollection
readonly
Table columns.
Instance Method Summary collapse
- #[](row_index, column_index = nil) ⇒ Object
-
#add(*text, **attributes) ⇒ Row
Created row.
-
#column_count ⇒ Integer
Count of columns.
-
#count ⇒ Integer
Count of rows.
- #delete(row) ⇒ Object
- #each(&block) ⇒ Object
- #each_cell_of(column_index) ⇒ Object
- #empty? ⇒ Boolean
Instance Attribute Details
#columns ⇒ ColumnsCollection (readonly)
Returns table columns.
265 266 267 |
# File 'lib/natty-ui/table.rb', line 265 def columns @columns end |
Instance Method Details
#[](row_index, column_index = nil) ⇒ Object
277 278 279 280 |
# File 'lib/natty-ui/table.rb', line 277 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.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/natty-ui/table.rb', line 283 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_count ⇒ Integer
Returns count of columns.
273 |
# File 'lib/natty-ui/table.rb', line 273 def column_count = @rows.empty? ? 0 : @rows.max_by(&:count).count |
#count ⇒ Integer
Returns count of rows.
270 |
# File 'lib/natty-ui/table.rb', line 270 def count = @rows.size |
#delete(row) ⇒ Object
300 301 302 |
# File 'lib/natty-ui/table.rb', line 300 def delete(row) row.is_a?(Row) ? @rows.delete(row) : @rows.delete_at(row) end |
#each(&block) ⇒ Object
275 |
# File 'lib/natty-ui/table.rb', line 275 def each(&block) = @rows.each(&block) |
#each_cell_of(column_index) ⇒ Object
304 305 306 307 308 309 |
# File 'lib/natty-ui/table.rb', line 304 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
267 |
# File 'lib/natty-ui/table.rb', line 267 def empty? = @rows.empty? |