class Proforma::PlainTextRenderer

A basic rendering engine that will output plain-text. It is meant to serve as an example of how to create a rendering engine for this library.

Constants

EXTENSION
RENDER_PREFIX

Attributes

column_separator[R]
line_length[R]
pane_separator[R]
writer[R]

Public Class Methods

new(column_separator: ', ', line_length: 40, pane_separator: ': ') click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 21
def initialize(column_separator: ', ', line_length: 40, pane_separator: ': ')
  @column_separator = column_separator.to_s
  @line_length      = line_length.to_i
  @pane_separator   = pane_separator.to_s
end

Public Instance Methods

render(prototype) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 27
def render(prototype)
  @writer = StringIO.new

  prototype.children.each do |child|
    method_name = child_method_name(child)

    raise ArgumentError, "Cannot render: #{method_name}" unless respond_to?(method_name, true)

    send(method_name, child)
  end

  Document.new(
    contents: writer.string,
    extension: EXTENSION,
    title: prototype.title
  )
end

Private Instance Methods

child_method_name(child) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 87
def child_method_name(child)
  name = child.class.name.split('::').last.downcase
  "#{RENDER_PREFIX}#{name}"
end
line(char) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 96
def line(char)
  char * line_length
end
render_banner(banner) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 49
def render_banner(banner)
  write_line(line('='))
  write_line(banner.title)
  write_line(line('='))
  write_line(banner.details)
  write_line(line('='))
end
render_header(header) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 57
def render_header(header)
  write_line(header.value.to_s.upcase)
end
render_pane(pane) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 79
def render_pane(pane)
  pane.columns.each do |column|
    column.lines.each do |line|
      write_line("#{line.label}#{pane_separator}#{line.value}")
    end
  end
end
render_separator(_separator) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 65
def render_separator(_separator)
  write_line(line('-'))
end
render_spacer(_separator) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 69
def render_spacer(_separator)
  write_line
end
render_table(table) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 73
def render_table(table)
  write_section(table.header)
  write_section(table.body)
  write_section(table.footer)
end
render_text(text) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 61
def render_text(text)
  write_line(text.value)
end
write_line(text = '') click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 92
def write_line(text = '')
  writer.puts(text)
end
write_section(section) click to toggle source
# File lib/proforma/plain_text_renderer.rb, line 100
def write_section(section)
  section.rows.each do |row|
    write_line(row.cells.map(&:text).join(column_separator))
  end
end