rabl-rails/lib/rabl-fast-json/library.rb

35 lines
978 B
Ruby

require 'singleton'
module RablFastJson
class Library
include Singleton
attr_accessor :view_renderer
def initialize
@cached_templates = {}
end
def get_rendered_template(source, context)
path = context.instance_variable_get(:@virtual_path)
@view_renderer = context.instance_variable_get(:@view_renderer)
compiled_template = get_compiled_template(path, source)
compiled_template.context = context
body = compiled_template.render
ActiveSupport::JSON.encode(compiled_template.has_root_name? ? { compiled_template.root_name => body } : body)
end
def get_compiled_template(path, source)
# @cached_templates[path] ||=
Compiler.new.compile_source(source)
end
def get(path)
template = @cached_templates[path]
return template if !template.nil?
t = @view_renderer.lookup_context.find_template(path, [], false)
get_compiled_template(path, t.source)
end
end
end