class Rabl::Renderer

Attributes

object[R]

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" }}'
options[R]

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" }}'

Public Class Methods

new(source, object = nil, options = {}) click to toggle source
# File lib/rabl/renderer.rb, line 25
def initialize(source, object = nil, options = {})
  options = {
    :format => :json,
    :scope => self,
    :view_path => [],
    :template => source
  }.update(options)

  @options = options
  @object = object

  engine.source = self.process_source(source)
end

Public Instance Methods

render(context_scope = nil) click to toggle source

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
# File lib/rabl/renderer.rb, line 46
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

Protected Instance Methods

engine() click to toggle source
# File lib/rabl/renderer.rb, line 55
def engine
  @engine ||= Rabl::Engine.new(nil, options)
end
model_name(object) click to toggle source

Internal: Returns the model name for an object

Example:

model_name(@post) => "@post"
# File lib/rabl/renderer.rb, line 82
def model_name(object)
  item = object.is_a?(Array) ? object.first : object
  name = item.class.name.underscore
  object.is_a?(Array) ? name.pluralize : name
end
process_source(source) click to toggle source

Returns the source given a relative template path

# File lib/rabl/renderer.rb, line 60
def process_source(source)
  return source if source.is_a?(String) && source =~ %r\n/
  source, _ = engine.fetch_source(source, { :view_path => options[:view_path] })
  source
end
set_instance_variable(object) click to toggle source

Internal: Sets an instance variable named after the class of `object`

Example:

object.class.name # => User
set_instance_variable(object) # => @user
# File lib/rabl/renderer.rb, line 72
def set_instance_variable(object)
  name = model_name(object).split('/').last
  instance_variable_set(:"@#{name}", object)
end