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.
116 |
# File 'lib/natty-ui/wrapper/table.rb', line 116 def [](row, col) = @rows.dig(row, col) |
#[]=(row, col, value) ⇒ Cell, ...
Change Cell or (text) value at a table position.
131 132 133 134 135 136 137 138 139 |
# File 'lib/natty-ui/wrapper/table.rb', line 131 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.
181 182 183 184 185 186 187 |
# File 'lib/natty-ui/wrapper/table.rb', line 181 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.
161 162 163 164 165 166 167 168 169 |
# File 'lib/natty-ui/wrapper/table.rb', line 161 def add_row(*columns, align: nil, style: nil) if columns.size == 1 && columns[0].respond_to?(:map) columns = columns[0] end columns = columns.map { |cell| as_cell(cell, align, style) if cell }.to_a @rows << (columns.empty? ? nil : columns) self end |
#align_column(column, alignment) ⇒ Table
Change text alignment of one or more column.
267 268 269 270 271 272 273 274 275 |
# File 'lib/natty-ui/wrapper/table.rb', line 267 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.
245 246 247 248 249 250 251 252 253 |
# File 'lib/natty-ui/wrapper/table.rb', line 245 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.
150 |
# File 'lib/natty-ui/wrapper/table.rb', line 150 def cell(value, align: :left, style: nil) = Cell.new(value, align, style) |
#col_count ⇒ Integer
Returns count of columns.
108 |
# File 'lib/natty-ui/wrapper/table.rb', line 108 def col_count = @rows.max_by { _1&.size || 0 }&.size || 0 |
#row_count ⇒ Integer
Returns count of rows.
105 |
# File 'lib/natty-ui/wrapper/table.rb', line 105 def row_count = @rows.size |
#style_column(column, style) ⇒ Table
Change style of one or more columns.
223 224 225 226 227 228 229 230 231 |
# File 'lib/natty-ui/wrapper/table.rb', line 223 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.
201 202 203 204 205 206 207 208 209 |
# File 'lib/natty-ui/wrapper/table.rb', line 201 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.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/natty-ui/wrapper/table.rb', line 281 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 |