Class: Rabl::Renderer
- Inherits:
-
Object
- Object
- Rabl::Renderer
- Defined in:
- lib/rabl/renderer.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(source, object = nil, options = {}) ⇒ Renderer
constructor
Public: Instantiate a new renderer This is a standalone class used for rendering rabl templates outside of a framework like Rails.
-
#render(context_scope = nil) ⇒ Object
Public: Actually render the template to the requested output format.
Constructor Details
#initialize(source, object = nil, options = {}) ⇒ Renderer
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" }}'
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rabl/renderer.rb', line 26 def initialize(source, object = nil, = {}) = { :format => :json, :scope => self, :view_path => [], :template => source }.merge() @options = @object = object engine.source = process_source(source) end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
14 15 16 |
# File 'lib/rabl/renderer.rb', line 14 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/rabl/renderer.rb', line 14 def @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 context_scope to the 'context_scope' object. Defaults to self.
Returns: And object representing the transformed object in the requested format.
e.g. json, xml, bson, plist
47 48 49 50 51 52 53 54 55 |
# File 'lib/rabl/renderer.rb', line 47 def render(context_scope = nil) context_scope ||= [:scope] || self set_object_instance_variable if context_scope == self locals = { :object => object }.merge(.fetch(:locals, {})) engine.apply(context_scope, locals).render end |