Class: CliFormat::Presenter::Space
- Extended by:
- Memoist
- Defined in:
- lib/cli_format/presenter/space.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
#empty_message, #header, #rows
Instance Method Summary collapse
- #format_row(row, max_widths) ⇒ Object
-
#formatted_row_separator ⇒ Object
interface method.
- #max_widths ⇒ Object
- #text ⇒ Object
Methods inherited from Base
Constructor Details
This class inherits a constructor from CliFormat::Presenter::Base
Instance Method Details
#format_row(row, max_widths) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/cli_format/presenter/space.rb', line 28 def format_row(row, max_widths) formatted_row = row.each_with_index.map do |col, idx| idx == row.size - 1 ? col.to_s : col.to_s.ljust(max_widths[idx]) end formatted_row.join(formatted_row_separator) end |
#formatted_row_separator ⇒ Object
interface method
37 38 39 |
# File 'lib/cli_format/presenter/space.rb', line 37 def formatted_row_separator " " end |
#max_widths ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/cli_format/presenter/space.rb', line 14 def max_widths all_rows = [@header, *@rows].compact max_widths = Array.new(all_rows[0].size, 0) all_rows.each do |row| row.each_with_index do |col, idx| max_widths[idx] = [max_widths[idx], col.to_s.length].max end end max_widths end |
#text ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/cli_format/presenter/space.rb', line 5 def text @buffer << format_row(@header, max_widths) if @header @rows.each do |row| @buffer << format_row(row, max_widths) end @buffer.join("\n") end |