Class: Marktable::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/marktable/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(markdown_table = '', headers: true) ⇒ Table

Returns a new instance of Table.



9
10
11
12
13
14
# File 'lib/marktable/table.rb', line 9

def initialize(markdown_table = '', headers: true)
  @headers = headers
  @rows = []
  @header_row = nil
  parse_content(markdown_table) unless markdown_table.empty?
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

Instance Method Details

#eachObject



16
17
18
19
20
21
22
# File 'lib/marktable/table.rb', line 16

def each
  if block_given?
    @rows.each { |row| yield(row) }
  else
    @rows.each
  end
end

#generateObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/marktable/table.rb', line 32

def generate
  return "" if @rows.empty?

  # Extract header keys or use first row data for header
  keys = header_keys
  
  # Calculate column widths considering both headers and all row values
  all_values = [keys] + @rows.map { |row| row.values }
  column_widths = calculate_column_widths(all_values)
  
  # Build the markdown table
  build_markdown_table(keys, column_widths)
end

#to_aObject



24
25
26
# File 'lib/marktable/table.rb', line 24

def to_a
  @rows.map { |row| row.data }
end

#to_sObject



28
29
30
# File 'lib/marktable/table.rb', line 28

def to_s
  generate
end