Class: Cadenza::BaseRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/cadenza/base_renderer.rb

Overview

BaseRenderer is a class you can use to more easily and cleanly implement your own rendering class. To use this then subclass BaseRenderer and implement the appropriate render_xyz methods (see #render for details).

Direct Known Subclasses

SourceRenderer, TextRenderer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_io) ⇒ BaseRenderer

creates a new renderer and assigns the given output io object to it

Parameters:

  • output_io (IO)

    the IO object which will be written to



11
12
13
# File 'lib/cadenza/base_renderer.rb', line 11

def initialize(output_io)
   @output = output_io
end

Instance Attribute Details

#outputIO (readonly)

Returns the io object that is being written to

Returns:

  • (IO)

    the io object that is being written to



7
8
9
# File 'lib/cadenza/base_renderer.rb', line 7

def output
  @output
end

Instance Method Details

#render(node, context, blocks = {}) ⇒ Object

renders the given document node to the output stream given in the constructor. this method will call the render_xyz method for the node given, where xyz is the demodulized underscored version of the node's class name. for example: given a Cadenza::DocumentNode this method will call render_document_node

Parameters:

  • node (Node)

    the node to render

  • context (Context)

    the context to render with

  • blocks (Hash) (defaults to: {})

    a mapping of the block names to the matching Cadenza::BlockNode. The blocks given should be rendered instead of blocks of the same name in the given document.



26
27
28
29
30
31
32
# File 'lib/cadenza/base_renderer.rb', line 26

def render(node, context, blocks={})
   node_type = node.class.name.split("::").last

   node_name = underscore(node_type).gsub!(/_node$/, '')

   send("render_#{node_name}", node, context, blocks)
end