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

34 lines
920 B
Ruby
Raw Normal View History

2012-02-27 13:49:34 +00:00
require 'singleton'
module RablFastJson
class Library
include Singleton
2012-03-02 13:39:20 +00:00
attr_accessor :view_renderer
2012-02-27 13:49:34 +00:00
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)
2012-03-02 10:32:11 +00:00
2012-02-27 13:49:34 +00:00
compiled_template = get_compiled_template(path, source, context)
compiled_template.context = context
2012-03-02 10:32:11 +00:00
ActiveSupport::JSON.encode(compiled_template.render)
2012-02-27 13:49:34 +00:00
end
def get_compiled_template(path, source, context)
2012-03-02 10:32:11 +00:00
@cached_templates[path] ||= Compiler.new(context).compile_source(source)
2012-02-27 13:49:34 +00:00
end
2012-03-02 10:32:11 +00:00
def get(path, context)
2012-02-27 13:49:34 +00:00
template = @cached_templates[path]
return template if !template.nil?
t = @view_renderer.lookup_context.find_template(path, [], false)
2012-03-02 10:32:11 +00:00
get_compiled_template(path, t.source, context)
2012-02-27 13:49:34 +00:00
end
end
end