Module: SycLink::Formatter
- Defined in:
- lib/syclink/formatter.rb
Overview
Methods to print data in a formatted way
Instance Method Summary collapse
-
#cut(string, size) ⇒ Object
Cuts the string down to the specified size.
-
#extract_columns(rows, header) ⇒ Object
Extracts the columns to display in the table based on the header column names.
-
#formatter_string(widhts, separator) ⇒ Object
Creates a formatter string based on the widths and the column separator.
-
#max_column_widths(columns, header) ⇒ Object
Determines max column widths for each column based on the data and header columns.
-
#print_header(header, formatter) ⇒ Object
Prints the table’s header.
-
#print_horizontal_line(line, separator, widths) ⇒ Object
Prints a horizontal line below the header.
-
#print_table(columns, formatter) ⇒ Object
Prints columns in a table format.
-
#table(rows, header) ⇒ Object
Based on the rows provided and the header values a table is printed.
Instance Method Details
#cut(string, size) ⇒ Object
Cuts the string down to the specified size
66 67 68 |
# File 'lib/syclink/formatter.rb', line 66 def cut(string, size) string[0..size-1] end |
#extract_columns(rows, header) ⇒ Object
Extracts the columns to display in the table based on the header column names
19 20 21 22 23 24 25 26 27 |
# File 'lib/syclink/formatter.rb', line 19 def extract_columns(rows, header) columns = [] header.each do |h| columns << rows.map do |r| r.send(h) end end columns end |
#formatter_string(widhts, separator) ⇒ Object
Creates a formatter string based on the widths and the column separator
44 45 46 47 48 |
# File 'lib/syclink/formatter.rb', line 44 def formatter_string(widhts, separator) widhts.map do |width| "%-#{width}s" end.join(separator) end |
#max_column_widths(columns, header) ⇒ Object
Determines max column widths for each column based on the data and header columns
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/syclink/formatter.rb', line 31 def max_column_widths(columns, header) row_column_widths = columns.map do |c| c.reduce(0) { |m, v| [m, v.length].max } end header_column_widths = header.map { |h| h.length } row_column_widths.zip(header_column_widths).map do |column| column.reduce(0) { |m, v| [m, v].max } end end |
#print_header(header, formatter) ⇒ Object
Prints the table’s header
51 52 53 |
# File 'lib/syclink/formatter.rb', line 51 def print_header(header, formatter) puts cut(sprintf(formatter, *header), 80) end |
#print_horizontal_line(line, separator, widths) ⇒ Object
Prints a horizontal line below the header
56 57 58 |
# File 'lib/syclink/formatter.rb', line 56 def print_horizontal_line(line, separator, widths) puts cut(widths.map { |width| line * width }.join(separator), 80) end |
#print_table(columns, formatter) ⇒ Object
Prints columns in a table format
61 62 63 |
# File 'lib/syclink/formatter.rb', line 61 def print_table(columns, formatter) columns.transpose.each { |row| puts cut(sprintf(formatter, *row), 80) } end |
#table(rows, header) ⇒ Object
Based on the rows provided and the header values a table is printed
8 9 10 11 12 13 14 15 |
# File 'lib/syclink/formatter.rb', line 8 def table(rows, header) columns = extract_columns(rows, header) widths = max_column_widths(columns, header) formatter = formatter_string(widths, " | ") print_header(header, formatter) print_horizontal_line("-", "-+-", widths) print_table(columns, formatter) end |