Module: ObjectTable::Printable
- Included in:
- TableMethods
- Defined in:
- lib/object_table/printable.rb
Class Method Summary collapse
Instance Method Summary collapse
- #_format_rows(rows, widths) ⇒ Object
- #_format_section(row_slice) ⇒ Object
- #inspect(max_section = 5, col_padding = 2) ⇒ Object
Class Method Details
.calc_column_widths(columns) ⇒ Object
11 12 13 |
# File 'lib/object_table/printable.rb', line 11 def self.calc_column_widths(columns) columns.map{|col| col.flatten.map(&:length).max} end |
.get_printable_column(column) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/object_table/printable.rb', line 3 def self.get_printable_column(column) column.shape[-1].times.map do |i| row = column[false, i] str = row.is_a?(NArray) ? row.inspect.partition("\n")[-1].strip : row.inspect str.split("\n") end end |
Instance Method Details
#_format_rows(rows, widths) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/object_table/printable.rb', line 25 def _format_rows(rows, widths) rows.flat_map do |row| height = row.map(&:length).max row = row.zip(widths).map do |cell, width| cell += [" "] * (height - cell.length) cell.map{|i| i.rjust(width)} end row.transpose.map(&:join) end end |
#_format_section(row_slice) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/object_table/printable.rb', line 15 def _format_section(row_slice) numbers = row_slice.map{|i| ["#{i}: "]} section = columns.map do |name, c| c = c.slice(false, row_slice) ObjectTable::Printable.get_printable_column(c) end [numbers] + section end |
#inspect(max_section = 5, col_padding = 2) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/object_table/printable.rb', line 38 def inspect(max_section = 5, col_padding = 2) header = "#{self.class}(#{nrows}, #{ncols})\n" return (header + "(empty table)") if ncols == 0 return (header + "(empty table with columns: #{colnames.join(", ")})") if nrows == 0 column_headers = [''] + colnames.map(&:to_s) if nrows > max_section * 2 head = _format_section(0 ... max_section) tail = _format_section((nrows - max_section) ... nrows) columns = [column_headers, head, tail].transpose widths = NArray.to_na(ObjectTable::Printable.calc_column_widths(columns)) + col_padding total_width = widths.sum rows = _format_rows(head.transpose, widths) rows.push('-' * total_width) rows += _format_rows(tail.transpose, widths) else section = _format_section(0...nrows) columns = [column_headers, section].transpose widths = NArray.to_na(ObjectTable::Printable.calc_column_widths(columns)) + col_padding rows = _format_rows(section.transpose, widths) end column_headers = _format_rows([[column_headers].transpose], widths).join header + ([column_headers] + rows + [column_headers]).join("\n") rescue NoMethodError => e raise Exception.new(e) end |