Skip to content

Commit

Permalink
Added Rabl::Renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvanderbyl committed Apr 14, 2012
1 parent 7aa3550 commit edb0378
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rabl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'rabl/engine'
require 'rabl/builder'
require 'rabl/configuration'
require 'rabl/renderer'
require 'rabl/railtie' if defined?(Rails) && Rails.version =~ /^3/

# Rabl.register!
Expand Down
4 changes: 4 additions & 0 deletions lib/rabl/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def initialize(source, options={})
@_options = options
end

def source=(string)
@_source = string
end

# Renders the representation based on source, object, scope and locals
# Rabl::Engine.new("...source...", { :format => "xml" }).render(scope, { :foo => "bar", :object => @user })
def render(scope, locals, &block)
Expand Down
31 changes: 31 additions & 0 deletions lib/rabl/renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Rabl
class Renderer < Engine

attr_reader :file, :object, :options
def initialize(file, object, options = {})
options = {:format => :json}.update(options)
@file, @object, @options = file, object, options

view_path = options.delete(:view_path)
source, location = engine.fetch_source(file, :view_path => view_path)
engine.source = source
end

def render
set_instance_variable(object)
engine.render(self, {})
end

protected

def engine
@engine ||= Engine.new(nil, options)
end

def set_instance_variable(object)
name = model_name(object).split('/').last
instance_variable_set(:"@#{name}", object)
end

end
end

0 comments on commit edb0378

Please sign in to comment.