Class: RablRails::Renderers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rabl-rails/renderers/base.rb

Direct Known Subclasses

JSON, PLIST, XML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, locals = nil) ⇒ Base

:nodoc:



8
9
10
11
12
13
# File 'lib/rabl-rails/renderers/base.rb', line 8

def initialize(context, locals = nil) # :nodoc:
  @_context = context
  @_options = {}
  @_resource = locals[:resource] if locals
  setup_render_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)

If a method is called inside a ‘node’ property or a ‘if’ lambda it will be passed to context if it exists or treated as a standard missing method.



118
119
120
# File 'lib/rabl-rails/renderers/base.rb', line 118

def method_missing(name, *args, &block)
  @_context.respond_to?(name) ? @_context.send(name, *args, &block) : super
end

Instance Attribute Details

#_optionsObject

Returns the value of attribute _options.



6
7
8
# File 'lib/rabl-rails/renderers/base.rb', line 6

def _options
  @_options
end

Instance Method Details

#format_output(hash) ⇒ Object

Format a hash into the desired output. Renderer subclasses must implement this method



40
41
42
# File 'lib/rabl-rails/renderers/base.rb', line 40

def format_output(hash)
  raise "Muse be implemented by renderer"
end

#render(template) ⇒ 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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rabl-rails/renderers/base.rb', line 21

def render(template)
  collection_or_resource = if template.data
    if @_context.respond_to?(template.data)
      @_context.send(template.data)
    else
      instance_variable_get(template.data)
    end
  end
  collection_or_resource ||= @_resource
  output_hash = collection_or_resource.respond_to?(:each) ? render_collection(collection_or_resource, template.source) :
    render_resource(collection_or_resource, template.source)
  _options[:root_name] = template.root_name
  format_output(output_hash)
end