Class: Marktable::Table
- Inherits:
-
Object
- Object
- Marktable::Table
- Includes:
- Enumerable
- Defined in:
- lib/marktable/table.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #each ⇒ Object
- #generate ⇒ Object
-
#initialize(markdown_table = '', headers: true) ⇒ Table
constructor
A new instance of Table.
- #to_a ⇒ Object
- #to_s ⇒ Object
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
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/marktable/table.rb', line 7 def headers @headers end |
Instance Method Details
#each ⇒ Object
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 |
#generate ⇒ Object
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_a ⇒ Object
24 25 26 |
# File 'lib/marktable/table.rb', line 24 def to_a @rows.map { |row| row.data } end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/marktable/table.rb', line 28 def to_s generate end |