Class: Cadenza::BaseRenderer
- Inherits:
-
Object
- Object
- Cadenza::BaseRenderer
- 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
Instance Attribute Summary collapse
-
#document ⇒ DocumentNode
readonly
deprecated
Deprecated.
temporary hack, will be removed later
-
#output ⇒ IO
readonly
The io object that is being written to.
Instance Method Summary collapse
-
#initialize(output_io) ⇒ BaseRenderer
constructor
creates a new renderer and assigns the given output io object to it.
-
#render(node, context, blocks = {}) ⇒ Object
renders the given document node to the output stream given in the constructor.
Constructor Details
#initialize(output_io) ⇒ BaseRenderer
creates a new renderer and assigns the given output io object to it
15 16 17 |
# File 'lib/cadenza/base_renderer.rb', line 15 def initialize(output_io) @output = output_io end |
Instance Attribute Details
#document ⇒ DocumentNode (readonly)
temporary hack, will be removed later
Returns the node which is at the root of the AST.
11 12 13 |
# File 'lib/cadenza/base_renderer.rb', line 11 def document @document end |
#output ⇒ IO (readonly)
Returns 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
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cadenza/base_renderer.rb', line 30 def render(node, context, blocks={}) #TODO: memoizing this is a terrible smell, add a "parent" hierarchy so # we can always find the root node from any node in the AST @document ||= node node_type = node.class.name.split("::").last node_name = underscore(node_type).gsub!(/_node$/, '') send("render_#{node_name}", node, context, blocks) end |