Module: RablRails::Renderers::Hash

Extended by:
Hash
Includes:
Helpers
Included in:
Hash, JSON, PLIST, XML
Defined in:
lib/rabl-rails/renderers/hash.rb

Instance Method Summary collapse

Methods included from Helpers

#collection?

Instance Method Details

#render(template, context, locals = nil) ⇒ Object

Render a template. Uses the compiled template source to get a hash with the actual data and then format the result according to the ‘format_result` method defined by the renderer.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rabl-rails/renderers/hash.rb', line 13

def render(template, context, locals = nil)
  visitor = Visitors::ToHash.new(context)

  collection_or_resource = if template.data
    if context.respond_to?(template.data)
      context.send(template.data)
    else
      visitor.instance_variable_get(template.data)
    end
  end

  render_with_cache(template.cache_key, collection_or_resource) do
    output_hash = if collection?(collection_or_resource)
      render_collection(collection_or_resource, template.nodes, visitor)
    else
      render_resource(collection_or_resource, template.nodes, visitor)
    end

    format_output(output_hash, root_name: template.root_name, params: context.params)
  end
end