class Convoy::Formatter::StringGrid
Constants
- DEFAULT_WIDTH
Attributes
column_count[R]
rows[RW]
width[R]
Public Class Methods
new(options = {}, &block)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 9 def initialize(options = {}, &block) @width = options[:width] || DEFAULT_WIDTH @column_count = options[:columns] || 3 @rows = [] block.call(self) if block_given? end
Public Instance Methods
row(*column_values)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 16 def row(*column_values) while column_values.size < @column_count column_values << '' end rows << column_values.map(&:to_s) end
to_s()
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 23 def to_s buffer = [] rows.each do |cells| virtual_row = normalize_virtual_row(virtual_row_for(cells)) physical_row_count_for(virtual_row).times do |physical_count| physical_row = format_physical_row_values(physical_row_for(virtual_row, physical_count)) buffer << physical_row.join("").chomp end end buffer.join("\n") end
Private Instance Methods
cell_value(value, column_index)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 103 def cell_value(value, column_index) sprintf("%-#{column_width(column_index)}s", value) end
column_width(column_index)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 68 def column_width(column_index) width = fair_column_width(column_index) if column_index == column_count - 1 width = last_column_width end width end
fair_column_width(index)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 76 def fair_column_width(index) width = values_in_column(index).map(&:length).max width = width + 1 width > max_column_width ? max_column_width : width end
format_physical_row_values(physical_row)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 37 def format_physical_row_values(physical_row) physical_row.each_with_index.map do |value, index| cell_value(value, index) end end
last_column_width()
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 82 def last_column_width full_fair_column_width = max_column_width * column_count + max_column_width_remainder all_but_last_fair_column_width = 0 (column_count - 1).times do |index| all_but_last_fair_column_width += fair_column_width(index) end full_fair_column_width - all_but_last_fair_column_width end
max_column_width()
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 95 def max_column_width width/column_count end
max_column_width_remainder()
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 99 def max_column_width_remainder width%column_count end
normalize_virtual_row(virtual_row)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 55 def normalize_virtual_row(virtual_row) virtual_row.map do |physical| while physical.size < physical_row_count_for(virtual_row) physical << "" end physical end end
physical_row_count_for(virtual_row)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 64 def physical_row_count_for(virtual_row) virtual_row.map { |physical| physical.size }.max end
physical_row_for(virtual_row, index)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 43 def physical_row_for(virtual_row, index) virtual_row.map { |physical| physical[index] } end
values_in_column(column_index)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 91 def values_in_column(column_index) rows.map { |cells| cells[column_index] } end
virtual_row_for(column_values)
click to toggle source
# File lib/convoy/formatter/string_grid.rb, line 47 def virtual_row_for(column_values) virtual_row = [] column_values.each_with_index do |cell, index| virtual_row << Convoy::Formatter::StringSplitter.new(column_width(index) - 1).split(cell) end normalize_virtual_row(virtual_row) end