class Rabl::Renderer

Attributes

object[R]
options[R]

Public Class Methods

new(source, object = nil, options = {}) click to toggle source

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" }}'
# File lib/rabl/renderer.rb, line 26
def initialize(source, object = nil, options = {})
  options = {
    :format     => :json,
    :scope      => self,
    :view_path  => [],
    :template   => source
  }.merge(options)

  @options  = options
  @object   = object

  engine.source = 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 context_scope to the 'context_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 47
def render(context_scope = nil)
  context_scope ||= options[:scope] || self

  set_object_instance_variable if context_scope == self

  locals = { :object => object }.merge(options.fetch(:locals, {}))

  engine.apply(context_scope, locals).render
end

Protected Instance Methods

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

Internal: Returns the model name for an object

Example:

object.class.name # => User
object_model_name => "user"
# File lib/rabl/renderer.rb, line 86
def object_model_name
  item = object

  is_collection = item.is_a?(Array)
  item = item.first if is_collection

  name = item.class.name.underscore

  name = name.pluralize if is_collection

  name.split("/").last
end
process_source(source) click to toggle source

Returns the source given a relative template path

# File lib/rabl/renderer.rb, line 63
def process_source(source)
  return source if source.is_a?(String) && source =~ /\n/

  source, _ = engine.fetch_source(source, { :view_path => options[:view_path] })
  source
end
set_object_instance_variable() click to toggle source

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

Example:

object.class.name # => User
set_object_instance_variable # => @user == object
# File lib/rabl/renderer.rb, line 76
def set_object_instance_variable
  instance_variable_set(:"@#{object_model_name}", object)
end