Class: Rabl::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/rabl/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, object = nil, options = {}) ⇒ Renderer

Returns a new instance of Renderer.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rabl/renderer.rb', line 25

def initialize(source, object = nil, options = {})
  options = {
    :format => :json,
    :scope => self,
    :view_path => []
  }.update(options)

  @options = options
  @object = object
  engine.source = self.process_source(source)
end

Instance Attribute Details

#objectObject (readonly)

Public: Instantiate a new renderer This is a standalone class used for rendering rabl templates outside of a framework like Rails. You may want to use this when using Rabl to render the request objects passed to message queues.

Example:

renderer = Rabl::Renderer.new('template_name', user, { :format => 'json', :view_path => 'app/views' })
renderer.render # => '{"user":{"name": "ivan" }}'


24
25
26
# File 'lib/rabl/renderer.rb', line 24

def object
  @object
end

#optionsObject (readonly)

Public: Instantiate a new renderer This is a standalone class used for rendering rabl templates outside of a framework like Rails. You may want to use this when using Rabl to render the request objects passed to message queues.

Example:

renderer = Rabl::Renderer.new('template_name', user, { :format => 'json', :view_path => 'app/views' })
renderer.render # => '{"user":{"name": "ivan" }}'


24
25
26
# File 'lib/rabl/renderer.rb', line 24

def options
  @options
end

Instance Method Details

#render(context_scope = nil) ⇒ Object

Public: Actually render the template to the requested output format.

  • context_scope:

    Override the render scope to the 'scope' object. Defaults to self.
    

Returns: And object representing the tranformed object in the requested format.

e.g. json, xml, bson, plist


44
45
46
47
48
49
# File 'lib/rabl/renderer.rb', line 44

def render(context_scope = nil)
  context_scope = context_scope ? context_scope : options.delete(:scope) || self
  set_instance_variable(object) if context_scope == self
  locals = options.fetch(:locals, {}).reverse_merge(:object => object)
  engine.render(context_scope, locals)
end