Class: DesignSystem::Components::Table

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

Overview

This is the class to define table component structure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTable

Returns a new instance of Table.



9
10
11
12
# File 'lib/design_system/components/table.rb', line 9

def initialize
  @columns = []
  @rows = []
end

Instance Attribute Details

#captionObject

Returns the value of attribute caption.



7
8
9
# File 'lib/design_system/components/table.rb', line 7

def caption
  @caption
end

#columnsObject

Returns the value of attribute columns.



7
8
9
# File 'lib/design_system/components/table.rb', line 7

def columns
  @columns
end

#rowsObject

Returns the value of attribute rows.



7
8
9
# File 'lib/design_system/components/table.rb', line 7

def rows
  @rows
end

Instance Method Details

#add_column(content, options = {}) ⇒ Object



14
15
16
# File 'lib/design_system/components/table.rb', line 14

def add_column(content, options = {})
  @columns << { content:, options: }
end

#add_numeric_column(content, options = {}) ⇒ Object



18
19
20
21
# File 'lib/design_system/components/table.rb', line 18

def add_numeric_column(content, options = {})
  default_options = { type: 'numeric' }
  @columns << { content:, options: default_options.merge(options) }
end

#add_row(*cells, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/design_system/components/table.rb', line 23

def add_row(*cells, &block)
  if block_given?
    row_builder = TableRowBuilder.new(@columns)
    block.call(row_builder)
    @rows << row_builder.cells
  else
    row = cells.map.with_index do |content, i|
      options = @columns[i][:options][:type] == 'numeric' ? { type: 'numeric' } : {}
      { content:, options: }
    end
    @rows << row
  end
end