Class: FormatOutput::ColumnBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/format_output/builders/column_builder.rb

Overview

A class to build columns for display.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ColumnBuilder

Prepare a blank page.



10
11
12
13
14
# File 'lib/format_output/builders/column_builder.rb', line 10

def initialize(options)
  @body        = ::FormatOutput.body(options)
  @pad         = ::FormatOutput.pad(options)
  @page_data   = []
end

Instance Method Details

#add(raw_item) ⇒ Object

Add an item to this page. Returns: The number of items that did not fit in the page.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/format_output/builders/column_builder.rb', line 18

def add(raw_item)
  item = raw_item.to_s
  fail "Item too large to fit." unless item.length < @body

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

  adjust_configuration
end

#renderObject

Render the page as an array of strings.



32
33
34
35
36
37
38
39
# File 'lib/format_output/builders/column_builder.rb', line 32

def render
  results, column_widths = [], get_column_widths

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

  @page_data.clear
  results
end