Class: XfOOrth::ColumnizedPage

Inherits:
Object
  • Object
show all
Defined in:
lib/fOOrth/library/formatting/columns.rb

Overview

A class to display data in columns.

Instance Method Summary collapse

Constructor Details

#initialize(page_length, page_width) ⇒ ColumnizedPage

Prepare a blank page.



9
10
11
12
# 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

Instance Method Details

#add(raw_item) ⇒ Object

Add an item to this page.
Returns

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



17
18
19
20
21
22
23
24
25
26
27
28
# 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 (slot = find_next_column)
    @page_data[slot] << item
  else
    @page_data << [item]
  end

  adjust_configuration
end

#renderObject

Render the page as an array of strings.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fOOrth/library/formatting/columns.rb', line 31

def render
  results = []
  widths  = @page_data.map {|column| column.foorth_column_width}

  (0...rows).each do |column_index|
    results << @page_data.each_with_index.map do |column, index|
      column[column_index].to_s.ljust(widths[index])
    end.join(" ").freeze
  end

  @page_data = []
  results
end