From 042609b5d4247ec8c0b10f3a23a8b7e4ca57d4f5 Mon Sep 17 00:00:00 2001 From: ccocchi Date: Tue, 24 Jul 2012 16:32:32 +0200 Subject: [PATCH] Add RablRails#render --- lib/rabl-rails/renderer.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/rabl-rails/renderer.rb b/lib/rabl-rails/renderer.rb index 0ee0acf..f149b4f 100644 --- a/lib/rabl-rails/renderer.rb +++ b/lib/rabl-rails/renderer.rb @@ -1,2 +1,24 @@ require 'rabl-rails/renderers/base' -require 'rabl-rails/renderers/json' \ No newline at end of file +require 'rabl-rails/renderers/json' + +module Renderer + mattr_reader :view_path + @@view_path = 'app/views' + + def render(object, template, options = {}) + format = options.delete(:format) || 'json' + source = find_template(template, format, options.delete(:view_path)) + compiled_template = Compiler.new.compile_source(source) + + # TODO: context needs to be set from options + Renderers.const_get(format.upcase!).new(context).render(compiled_template) + end + + private + + def find_template(name, format, view_path = nil) + view_path ||= self.view_path + path = File.join(view_path, "#{name}.#{format}.rabl") + File.exists?(path) : File.read(path) : nil + end +end \ No newline at end of file