Method: Marktable.table
- Defined in:
- lib/marktable.rb
.table(array, headers: true) ⇒ Object
Convert an array to a Marktable::Table
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/marktable.rb', line 82 def self.table(array, headers: true) table = Table.new([], headers: headers) # Ensure all data values are strings string_array = array.map do |row| # Handle Row instances by extracting their data if row.is_a?(Row) row.data elsif row.is_a?(Hash) row.transform_values(&:to_s) else row.map(&:to_s) end end if headers && string_array.first.is_a?(Hash) header_keys = string_array.first.keys table.instance_variable_set(:@header_row, header_keys.each_with_object({}) { |k, h| h[k] = k }) table.instance_variable_set(:@rows, string_array.map { |row_data| Row.new(row_data, headers: header_keys) }) else table.instance_variable_set(:@rows, string_array.map { |row_data| Row.new(row_data, headers: nil) }) end table end |