class XfOOrth::ColumnizedPage

A class to display data in columns.

Public Class Methods

new(page_length, page_width) click to toggle source

Prepare a blank page.

# File lib/fOOrth/library/formatting/columns.rb, line 9
def initialize(page_length, page_width)
  @page_length, @page_width = page_length, page_width
  @page_data = []
end

Public Instance Methods

add(raw_item) click to toggle source

Add an item to this page.
Returns

  • The number if items that did not fit in the page.

# File lib/fOOrth/library/formatting/columns.rb, line 17
def add(raw_item)
  item = raw_item.to_s
  fail "Item too large to fit." unless item.length < @page_width

  if (column = find_next_column)
    @page_data[column] << item
  else
    @page_data << [item]
  end

  adjust_configuration
end
render() click to toggle source

Render the page as an array of strings.

# File lib/fOOrth/library/formatting/columns.rb, line 31
def render
  results, column_widths = [], get_column_widths

  rows.times { |row_index| results << render_row(row_index, column_widths)}

  @page_data.clear
  results
end

Private Instance Methods

add_a_row() click to toggle source

Add a row to the page, moving items as needed.

# File lib/fOOrth/library/formatting/columns.rb, line 67
def add_a_row
  target = rows + 1
  pool, @page_data = @page_data.flatten, []

  until pool.empty?
    @page_data << pool.shift(target)
  end
end
adjust_configuration() click to toggle source

Make sure the page fits within its boundaries.
Returns

  • The number if items that did not fit in the page.

# File lib/fOOrth/library/formatting/columns.rb, line 57
def adjust_configuration
  while total_width >= @page_width
    return @page_data.pop.length if rows == @page_length
    add_a_row
  end

  0
end
empty?() click to toggle source

Is this page empty?

# File lib/fOOrth/library/formatting/columns.rb, line 103
def empty?
  @page_data.empty?
end
find_next_column() click to toggle source

Which column will receive the next item?
Returns

  • The index of the first non-filled column or nil if none found.

# File lib/fOOrth/library/formatting/columns.rb, line 110
def find_next_column
  (1...(@page_data.length)).each do |index|
    if @page_data[index].length < @page_data[index-1].length
      return index
    end
  end

  nil
end
fits?() click to toggle source

Does the data fit on the page?

# File lib/fOOrth/library/formatting/columns.rb, line 93
def fits?
  total_width < @page_width
end
get_column_widths() click to toggle source

Get the widths of all columns

# File lib/fOOrth/library/formatting/columns.rb, line 43
def get_column_widths
  @page_data.map {|column| column.foorth_column_width}
end
render_row(row_index, widths) click to toggle source

Render a single row of data.

# File lib/fOOrth/library/formatting/columns.rb, line 48
def render_row(row_index, widths)
  @page_data.each_with_index.map do |column, index|
    column[row_index].to_s.ljust(widths[index])
  end.join(" ")
end
rows() click to toggle source

How many rows are currently in this page?

# File lib/fOOrth/library/formatting/columns.rb, line 98
def rows
  empty? ? 0 : @page_data[0].length
end
total_width() click to toggle source

Compute the total width of all of the columns.
Returns

  • The sum of the widths of the widest items of each column plus a space between each of those columns.

# File lib/fOOrth/library/formatting/columns.rb, line 80
def total_width
  if empty?
    0
  else
    #The starting point, @page_data.length-1, represents the spaces needed
    #between the columns. So N columns means N-1 spaces.
    @page_data.inject(@page_data.length-1) do |sum, column|
      sum + column.foorth_column_width
    end
  end
end