Class: NattyUI::Features::Table
- Inherits:
-
Object
- Object
- NattyUI::Features::Table
- Defined in:
- lib/natty-ui/wrapper/table.rb
Overview
Helper class to define a table layout.
Defined Under Namespace
Classes: Cell
Instance Method Summary collapse
-
#[](row, col) ⇒ Cell?
Get the Cell at a table position.
-
#[]=(row, col, value) ⇒ Cell, ...
Change Cell or (text) value at a table position.
-
#add_column(*rows, align: nil, style: nil) ⇒ Table
Add a new column to the table.
-
#add_row(*columns, align: nil, style: nil) ⇒ Table
(also: #add)
Add a new row to the table.
-
#align_column(column, alignment) ⇒ Table
Change text alignment of one or more column.
-
#align_row(row, alignment) ⇒ Table
Change text alignment of one or more rows.
-
#cell(value, align: :left, style: nil) ⇒ Cell
Create a new cell.
-
#col_count ⇒ Integer
Count of columns.
-
#row_count ⇒ Integer
Count of rows.
-
#style_column(column, style) ⇒ Table
Change style of one or more columns.
-
#style_row(row, style) ⇒ Table
Change style of one or more rows.
-
#to_a ⇒ Array<Array<Cell>>
Convert the table to the compactest (two-dimensional) array representation.
Instance Method Details
#[](row, col) ⇒ Cell?
Get the Cell at a table position.
113 |
# File 'lib/natty-ui/wrapper/table.rb', line 113 def [](row, col) = @rows.dig(row, col) |
#[]=(row, col, value) ⇒ Cell, ...
Change Cell or (text) value at a table position.
128 129 130 131 132 133 134 135 136 |
# File 'lib/natty-ui/wrapper/table.rb', line 128 def []=(row, col, value) row = (@rows[row] ||= []) if value.nil? || value.is_a?(Cell) row[col] = value else cell = row[col] cell ? cell.value = value : row[col] = Cell.new(value, nil, nil) end end |
#add_column(*rows, align: nil, style: nil) ⇒ Table
Add a new column to the table.
177 178 179 180 181 182 183 |
# File 'lib/natty-ui/wrapper/table.rb', line 177 def add_column(*rows, align: nil, style: nil) rows = rows[0] if rows.size == 1 && rows[0].respond_to?(:map) row_idx = -1 rows.each do |cell| (@rows[row_idx += 1] ||= []) << as_cell(cell, align, style) end end |
#add_row(*columns, align: nil, style: nil) ⇒ Table Also known as: add
Add a new row to the table.
158 159 160 161 162 163 164 165 |
# File 'lib/natty-ui/wrapper/table.rb', line 158 def add_row(*columns, align: nil, style: nil) if columns.size == 1 && columns[0].respond_to?(:map) columns = columns[0] end columns = columns.map { as_cell(_1, align, style) if _1 }.to_a @rows << (columns.empty? ? nil : columns) self end |
#align_column(column, alignment) ⇒ Table
Change text alignment of one or more column.
263 264 265 266 267 268 269 270 271 |
# File 'lib/natty-ui/wrapper/table.rb', line 263 def align_column(column, alignment) if column.is_a?(Integer) column = [column] elsif !column.is_a?(Enumerable) raise(TypeError, "invalid column value - #{column}") end @rows.each { |row| column.each { row[_1]&.align = alignment } } self end |
#align_row(row, alignment) ⇒ Table
Change text alignment of one or more rows.
241 242 243 244 245 246 247 248 249 |
# File 'lib/natty-ui/wrapper/table.rb', line 241 def align_row(row, alignment) if row.is_a?(Integer) row = [row] elsif !row.is_a?(Enumerable) raise(TypeError, "invalid row value - #{row}") end row.each { |r| @rows[r]&.each { _1&.align = alignment } } self end |
#cell(value, align: :left, style: nil) ⇒ Cell
Create a new cell.
147 |
# File 'lib/natty-ui/wrapper/table.rb', line 147 def cell(value, align: :left, style: nil) = Cell.new(value, align, style) |
#col_count ⇒ Integer
Returns count of columns.
105 |
# File 'lib/natty-ui/wrapper/table.rb', line 105 def col_count = @rows.max_by { _1&.size || 0 }&.size || 0 |
#row_count ⇒ Integer
Returns count of rows.
102 |
# File 'lib/natty-ui/wrapper/table.rb', line 102 def row_count = @rows.size |
#style_column(column, style) ⇒ Table
Change style of one or more columns.
219 220 221 222 223 224 225 226 227 |
# File 'lib/natty-ui/wrapper/table.rb', line 219 def style_column(column, style) if column.is_a?(Integer) column = [column] elsif !column.is_a?(Enumerable) raise(TypeError, "invalid column value - #{column}") end @rows.each { |row| column.each { row[_1]&.style = style } } self end |
#style_row(row, style) ⇒ Table
Change style of one or more rows.
197 198 199 200 201 202 203 204 205 |
# File 'lib/natty-ui/wrapper/table.rb', line 197 def style_row(row, style) if row.is_a?(Integer) row = [row] elsif !row.is_a?(Enumerable) raise(TypeError, "invalid row value - #{row}") end row.each { |r| @rows[r]&.each { _1&.style = style } } self end |
#to_a ⇒ Array<Array<Cell>>
Convert the table to the compactest (two-dimensional) array representation.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/natty-ui/wrapper/table.rb', line 277 def to_a ret = [] ridx = -1 @rows.each do |row| ridx += 1 next unless row count = 0 row = row.map do |cell| next unless cell next if cell.value.empty? count += 1 cell.dup end ret[ridx] = row if count.positive? end ret end |