Class: PassStation::Output::Table
- Inherits:
-
Object
- Object
- PassStation::Output::Table
- Defined in:
- lib/pass_station/output.rb
Overview
Simple table formatter
Direct Known Subclasses
Class Method Summary collapse
-
.colsize_count(table, column) ⇒ Integer
Calculate column size (max item size).
-
.colsizes_count(table) ⇒ Hash
Calculate the size of all columns (max item size).
-
.correct_min_colsizes(colsizes) ⇒ Hash
Correct colsizes to be at least of the size of the headers (case when values are shorter than headers and breaks the table display).
-
.format(table) ⇒ Array<String>
Format the
Array<CSV::Row>
into a simple table with justified columns. -
.headers(colsizes) ⇒ String
Generate justified headers.
-
.justify(row, column, colsizes) ⇒ String
Left justify an element of the column.
-
.justify_row(row, colsizes) ⇒ String
Left justify all elements of the column.
Class Method Details
.colsize_count(table, column) ⇒ Integer
Calculate column size (max item size)
88 89 90 |
# File 'lib/pass_station/output.rb', line 88 def colsize_count(table, column) table.map { |i| i[column].nil? ? 0 : i[column].size }.max + 1 end |
.colsizes_count(table) ⇒ Hash
Calculate the size of all columns (max item size)
95 96 97 98 99 100 |
# File 'lib/pass_station/output.rb', line 95 def colsizes_count(table) colsizes = table.first.to_h.keys.each_with_object({}) do |c, h| h[c] = colsize_count(table, c) end correct_min_colsizes(colsizes) end |
.correct_min_colsizes(colsizes) ⇒ Hash
Correct colsizes to be at least of the size of the headers (case when values are shorter than headers and breaks the table display)
106 107 108 109 110 111 112 |
# File 'lib/pass_station/output.rb', line 106 def correct_min_colsizes(colsizes) min_colsizes = { productvendor: 14, username: 9, password: 9, modelsoftware_name: 19, version: 8, access_type: 12, privileges: 11, notes: 6, vendor: 7 } colsizes.each_with_object({}) { |(k, v), h| h[k] = [v, min_colsizes[k]].max } end |
.format(table) ⇒ Array<String>
Format the Array<CSV::Row>
into a simple table with justified columns
74 75 76 77 78 79 80 81 82 |
# File 'lib/pass_station/output.rb', line 74 def format(table) out = [] colsizes = colsizes_count(table) out.push(headers(colsizes)) table.each do |r| out.push(justify_row(r, colsizes)) end out end |
.headers(colsizes) ⇒ String
Generate justified headers
138 139 140 |
# File 'lib/pass_station/output.rb', line 138 def headers(colsizes) colsizes.map { |k, v| k.to_s.ljust(v) }.join.to_s end |
.justify(row, column, colsizes) ⇒ String
Left justify an element of the column
119 120 121 |
# File 'lib/pass_station/output.rb', line 119 def justify(row, column, colsizes) row[column].to_s.ljust(colsizes[column]) end |
.justify_row(row, colsizes) ⇒ String
Left justify all elements of the column
127 128 129 130 131 132 133 |
# File 'lib/pass_station/output.rb', line 127 def justify_row(row, colsizes) out = '' row.to_h.each_key do |col| out += justify(row, col, colsizes) end out end |