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 =
12- 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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tabulo/table.rb', line 39 def initialize(sources, = { }) opts = { columns: [], column_width: DEFAULT_COLUMN_WIDTH, 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 = opts[: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.
95 96 97 |
# File 'lib/tabulo/table.rb', line 95 def add_column(label, = { }, &extractor) @columns << make_column(label, .merge(extractor: extractor)) end |
#each ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/tabulo/table.rb', line 118 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.
134 135 136 |
# File 'lib/tabulo/table.rb', line 134 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.
147 148 149 |
# File 'lib/tabulo/table.rb', line 147 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.
101 102 103 104 105 106 107 |
# File 'lib/tabulo/table.rb', line 101 def to_s if @columns.any? join_lines(map(&:to_s)) else "" end end |