Class: DesignSystem::Components::TableRowBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/design_system/components/table.rb

Overview

This class helps in building the row for the table <tr>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns) ⇒ TableRowBuilder

Returns a new instance of TableRowBuilder.



42
43
44
45
# File 'lib/design_system/components/table.rb', line 42

def initialize(columns)
  @cells = []
  @columns = columns
end

Instance Attribute Details

#cellsObject (readonly)

Returns the value of attribute cells.



40
41
42
# File 'lib/design_system/components/table.rb', line 40

def cells
  @cells
end

Instance Method Details

#add_cell(content = nil, options = {}, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/design_system/components/table.rb', line 47

def add_cell(content = nil, options = {}, &block)
  if block_given?
    options = content || {}
    content = block
  end

  index = @cells.size

  if index >= @columns.size
    raise ArgumentError,
          "Too many cells in row (expected at most #{@columns.size}, got #{index + 1})"
  end

  if @columns[index][:options][:type] == 'numeric'
    default_options = { type: 'numeric' }
    options = default_options.merge(options)
  end
  @cells << { content:, options: }
end