Class: RablRails::Library

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rabl-rails/library.rb

Instance Method Summary collapse

Constructor Details

#initializeLibrary

Returns a new instance of Library.



7
8
9
# File 'lib/rabl-rails/library.rb', line 7

def initialize
  @cached_templates = {}
end

Instance Method Details

#compile_template_from_path(path) ⇒ Object



30
31
32
33
34
35
# File 'lib/rabl-rails/library.rb', line 30

def compile_template_from_path(path)
  template = @cached_templates[path]
  return template if template
  t = @lookup_context.find_template(path, [], false)
  compile_template_from_source(t.source, path)
end

#compile_template_from_source(source, path = nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/rabl-rails/library.rb', line 21

def compile_template_from_source(source, path = nil)
  if path && RablRails.cache_templates?
    @cached_templates[path] ||= Compiler.new.compile_source(source)
    @cached_templates[path].dup
  else
    Compiler.new.compile_source(source)
  end
end

#get_rendered_template(source, context, locals = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/rabl-rails/library.rb', line 11

def get_rendered_template(source, context, locals = nil)
  path = context.instance_variable_get(:@virtual_path)
  @lookup_context = context.lookup_context

  compiled_template = compile_template_from_source(source, path)

  format = context.params[:format] || 'json'
  Renderers.const_get(format.upcase!).new(context, locals).render(compiled_template)
end