Class: Tabulo::Table
Overview
Represents a table primarily intended for “pretty-printing” in a fixed-width font.
A Table is also an Enumerable, of which each element is a Row.
Constant Summary collapse
- DEFAULT_COLUMN_WIDTH =
8- HORIZONTAL_RULE_CHARACTER =
"-"- CORNER_CHARACTER =
"+"
Instance Method Summary collapse
-
#add_column(label, options = { }, &extractor) ⇒ Object
Adds a column to the Table.
- #each ⇒ Object
-
#formatted_header ⇒ String
An “ASCII” graphical representation of the Table column headers.
-
#horizontal_rule ⇒ String
An “ASCII” graphical representation of a horizontal dividing line suitable for printing at any point in the table.
-
#initialize(sources, options = { }) {|_self| ... } ⇒ Table
constructor
A new Table.
-
#to_s ⇒ String
A graphical “ASCII” representation of the Table, suitable for display in a fixed-width font.
Constructor Details
#initialize(sources, options = { }) {|_self| ... } ⇒ Table
Returns a new Table.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tabulo/table.rb', line 37 def initialize(sources, = { }) opts = { columns: [], header_frequency: :start, # nil to wrap to no max, 1 to wrap to 1 row then truncate, etc.. wrap_header_cells_to: nil, wrap_body_cells_to: nil }.merge() @header_frequency = opts[:header_frequency] @wrap_header_cells_to = opts[:wrap_header_cells_to] @wrap_body_cells_to = opts[:wrap_body_cells_to] @sources = sources @joiner = "|" @truncation_indicator = "~" @padding_character = " " @default_column_width = DEFAULT_COLUMN_WIDTH @columns = opts[:columns].map { |item| make_column(item) } yield self if block_given? end |
Instance Method Details
#add_column(label, options = { }, &extractor) ⇒ Object
Adds a column to the Table.
91 92 93 |
# File 'lib/tabulo/table.rb', line 91 def add_column(label, = { }, &extractor) @columns << make_column(label, extractor: extractor) end |
#each ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/tabulo/table.rb', line 110 def each @sources.each_with_index do |source, index| include_header = case @header_frequency when :start index == 0 when Fixnum index % @header_frequency == 0 else @header_frequency end yield body_row(source, with_header: include_header) end end |
#formatted_header ⇒ String
Returns an “ASCII” graphical representation of the Table column headers.
126 127 128 |
# File 'lib/tabulo/table.rb', line 126 def formatted_header format_row(true, &:header_cell) end |
#horizontal_rule ⇒ String
Returns an “ASCII” graphical representation of a horizontal dividing line suitable for printing at any point in the table.
139 140 141 |
# File 'lib/tabulo/table.rb', line 139 def horizontal_rule format_row(false, HORIZONTAL_RULE_CHARACTER, CORNER_CHARACTER, &:horizontal_rule) end |
#to_s ⇒ String
Returns a graphical “ASCII” representation of the Table, suitable for display in a fixed-width font.
97 98 99 |
# File 'lib/tabulo/table.rb', line 97 def to_s join_lines(map(&:to_s)) end |