Class: Proforma::PlainTextRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/proforma/plain_text_renderer.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_separator: ', ', line_length: 40, pane_separator: ': ') ⇒ PlainTextRenderer

Returns a new instance of PlainTextRenderer.



21
22
23
24
25
# 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

Instance Attribute Details

#column_separatorObject (readonly)

Returns the value of attribute column_separator.



19
20
21
# File 'lib/proforma/plain_text_renderer.rb', line 19

def column_separator
  @column_separator
end

#line_lengthObject (readonly)

Returns the value of attribute line_length.



19
20
21
# File 'lib/proforma/plain_text_renderer.rb', line 19

def line_length
  @line_length
end

#pane_separatorObject (readonly)

Returns the value of attribute pane_separator.



19
20
21
# File 'lib/proforma/plain_text_renderer.rb', line 19

def pane_separator
  @pane_separator
end

Instance Method Details

#render(prototype) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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