class TablePuts::Printer

Attributes

data[R]
max_width[R]
min_width[R]
transposed[R]

Public Class Methods

new(data, min_width, max_width) click to toggle source
# File lib/table_puts/printer.rb, line 4
def initialize(data, min_width, max_width)
  @data = data
  @transposed = transpose_data
  @min_width = min_width
  @max_width = max_width
  check_widths(min_width, max_width)
end

Public Instance Methods

abbreviate(string) click to toggle source
# File lib/table_puts/printer.rb, line 86
def abbreviate(string)
  string == string[0...max_width] ? string : string[0...max_width-3] + "..."
end
break_string() click to toggle source
# File lib/table_puts/printer.rb, line 59
def break_string
  @break_string ||= column_widths.map { |size| "+-#{ '-'*size }-" }.join + "+"
end
calculate_column_justification() click to toggle source

Computes how we should justify the values in each column

If all values in a column contain a number or are nil, then we right justify Otherwise, left justify

Returns an array of 'r's and 'l's for each column

# File lib/table_puts/printer.rb, line 80
def calculate_column_justification
  transposed.map do |(key, values)|
    values.all? { |entry| entry.to_s.match(/\d/) || entry.nil? } ? 'r' : 'l'
  end
end
call() click to toggle source
# File lib/table_puts/printer.rb, line 12
def call
  puts break_string
  puts header
  puts break_string
  puts rows
  puts break_string
end
check_widths(min, max) click to toggle source
# File lib/table_puts/printer.rb, line 90
def check_widths(min, max)
  if min > max
    puts "WARNING: max_width is greater than min_width"
    puts "         This may cause unexpected output"
    puts "         min_width: #{ min_width }\tmax_width: #{ max_width }"
  end
end
column_widths() click to toggle source

An array of how many characters wide each column should be

You can set a minimum and maximum width for all columns by passing in min_width and max_width

# File lib/table_puts/printer.rb, line 67
def column_widths
  @column_widths ||= transposed.map do |value_array|
    largest_width_in_column = value_array.flatten.map { |entry| entry.to_s.length }.max
    [[largest_width_in_column, max_width].min, min_width].max
  end
end
header() click to toggle source
# File lib/table_puts/printer.rb, line 39
def header
  transposed.keys.map.with_index do |key, i|
    key_string = abbreviate(key.to_s).ljust(column_widths[i])

    "| #{ key_string } "
  end.join + "|"
end
rows() click to toggle source
# File lib/table_puts/printer.rb, line 47
def rows
  justification = calculate_column_justification
  data.map do |row|
    row.map.with_index do |(key, value), i|
      justify = "#{ justification[i] }just".to_sym
      value_string = abbreviate(value.to_s).send(justify, (column_widths[i]))

      "| #{ value_string } "
    end.join + "|"
  end
end
transpose_data() click to toggle source

Transforms data into a single hash with each value containing an array of the values from each hash - for example:

{ x: 1, y: 2 }, { x: 3 }, { y: 4 }

becomes { x: [1, 3], y: [2, 4] }

# File lib/table_puts/printer.rb, line 27
def transpose_data
  transposed_data = {}
  data.first.keys.map { |key| transposed_data[key] = [] }

  data.each do |hash|
    hash.each do |key,value|
      transposed_data[key] << value
    end
  end
  transposed_data
end