Class: TypedPrint::Table
- Inherits:
-
Object
- Object
- TypedPrint::Table
- Defined in:
- lib/typed_print/table.rb
Instance Method Summary collapse
-
#initialize(data, alignments = {}, only_columns = nil, custom_headers = {}) ⇒ Table
constructor
A new instance of Table.
- #render(format = :plain) ⇒ Object
- #render_markdown ⇒ Object
- #render_plain ⇒ Object
Constructor Details
#initialize(data, alignments = {}, only_columns = nil, custom_headers = {}) ⇒ Table
Returns a new instance of Table.
5 6 7 8 9 10 11 12 |
# File 'lib/typed_print/table.rb', line 5 def initialize(data, alignments = {}, only_columns = nil, custom_headers = {}) @data = data @alignments = alignments @only_columns = only_columns&.map(&:to_sym) @custom_headers = custom_headers.transform_keys(&:to_sym) @headers = determine_headers end |
Instance Method Details
#render(format = :plain) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/typed_print/table.rb', line 14 def render(format = :plain) if format == :markdown render_markdown else render_plain end end |
#render_markdown ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/typed_print/table.rb', line 56 def render_markdown return "" if @data.empty? # Build rows rows = @data.map { |row| @headers.map { |h| format_value(row[h]) } } # Build header strings header_strings = @headers.map do |h| if @custom_headers[h] @custom_headers[h] else h.to_s.split('_').map(&:capitalize).join(' ') end end # Calculate column widths for consistent spacing column_widths = header_strings.map(&:length) rows.each do |row| row.each_with_index do |cell, i| column_widths[i] = [column_widths[i], cell.length].max end end # Build markdown table output = [] # Header row output << "| " + header_strings.each_with_index.map { |h, i| h.ljust(column_widths[i]) }.join(" | ") + " |" # Separator row output << "|" + column_widths.map { |w| "-" * (w + 2) }.join("|") + "|" # Data rows rows.each do |row| output << "| " + row.each_with_index.map { |cell, i| cell.ljust(column_widths[i]) }.join(" | ") + " |" end output.join("\n") end |
#render_plain ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/typed_print/table.rb', line 22 def render_plain return "" if @data.empty? # Build rows rows = @data.map { |row| @headers.map { |h| format_value(row[h]) } } # Build header strings (with custom headers or capitalized defaults) header_strings = @headers.map do |h| if @custom_headers[h] @custom_headers[h] else h.to_s.split('_').map(&:capitalize).join(' ') end end # Calculate column widths column_widths = header_strings.map(&:length) rows.each do |row| row.each_with_index do |cell, i| column_widths[i] = [column_widths[i], cell.length].max end end # Build output output = [] output << format_row(header_strings, column_widths, :center) output << separator(column_widths) rows.each do |row| output << format_row(row, column_widths) end output.join("\n") end |