Class: Cadenza::SourceRenderer

Inherits:
BaseRenderer show all
Defined in:
lib/cadenza/source_renderer.rb

Overview

SourceRenderer is a rendering implementation that turns a Cadenza AST back into Cadenza source code.

This is mainly intended for users who wish to migrate templates stored in databases, or other storage devices, as their product progresses.

For example: if in v1.0 of your app you define a filter X and deprecate it in favour of filter Y you can use this renderer to automatically replace instances of filter X with filter Y

I'm sure there are many other exciting use cases for the imaginitive user, feel free to let me know what else you do with it!

Constant Summary

IllegalStateError =

This exception is raised when you try to transition to an undefined state

Class.new(RuntimeError)
IllegalStateTransitionError =

This exception is raised when you try to transition from one state to another which is not allowed

Class.new(RuntimeError)
ValidStates =

A list of all valid states for the renderer

[:text, :var, :tag]

Instance Attribute Summary collapse

Attributes inherited from BaseRenderer

#output

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseRenderer

#render

Constructor Details

#initialize(*args) ⇒ SourceRenderer

creates a new Cadenza::SourceRenderer and places it into the :text state



42
43
44
45
# File 'lib/cadenza/source_renderer.rb', line 42

def initialize(*args)
   @state = :text
   super
end

Instance Attribute Details

#stateObject

returns the current state of the renderer (see #ValidStates)



29
30
31
# File 'lib/cadenza/source_renderer.rb', line 29

def state
  @state
end

Class Method Details

.render(document_node, context = {}) ⇒ Object

Renders the document given with the given context directly to a string returns.

Parameters:

  • document_node (DocumentNode)

    the root of the AST you want to render.

  • context (Context) (defaults to: {})

    the context object to render the document with



35
36
37
38
39
# File 'lib/cadenza/source_renderer.rb', line 35

def self.render(document_node, context={})
   io = StringIO.new
   new(io).render(document_node, context)
   io.string
end