Browse files

Fixes for sinatra

  • Loading branch information...
1 parent 6900991 commit a2f1b84a7d8728c164e2e07e4bfef73f54b4f0c6 @paul committed Jun 20, 2010
Showing with 54 additions and 10 deletions.
  1. +18 −0 lib/rest-json-template/builder.rb
  2. +36 −10 lib/rest-json-template/sinatra.rb
View
18 lib/rest-json-template/builder.rb
@@ -3,6 +3,24 @@
module RestJson
+ class Engine
+
+ def initialize(data, options = {})
+ @data = data
+ @skip_encoding = options[:skip_encoding]
+ @builder = Builder.new(data)
+ end
+
+ def render(scope, locals, &block)
+ output = @builder.render(
+ if @skip_encoding
+ @builder.render(scope, locals).attr
+
+ end
+
+ end
+ end
+
class Builder
def initialize(resource)
View
46 lib/rest-json-template/sinatra.rb
@@ -1,24 +1,50 @@
require 'tilt'
-module RestJson
- module SinatraHelper
- def restjson_for(resource, &blk)
- builder = RestJson::Builder.new(resource)
- yield builder
- builder
+module Sinatra
+ module RestfulJson
+
+ module Helpers
+
+ def rfj_collection(resources)
+ json = {
+ :href => request.url,
+ :items => resources.map { |resource| rfj(resource, :skip_encoding => true) }
+ }
+
+ response['Content-Type'] = "application/json"
+ json = Yajl::Encoder.encode(json)
+ end
+
+ def rfj(resource, options = {})
+ template = resource.class.to_s.downcase
+ p template
+ render :rfj, template, options, locals
+ end
+
+ end
+
+ def self.registered(app)
+ app.helpers(RestfulJson::Helpers)
end
+
end
- Sinatra.helpers(SinatraHelper)
+ register RestfulJson
+end
+
+module RestfulJson
class Template < Tilt::Template
- def compile!
+ def prepare
end
def evaluate(scope, locals, &block)
- scope.instance_eval(data)
+ code = ::RestJson::Engine.new
+
end
+
+
end
- Tilt.register('restjson', Template)
+ Tilt.register(:rfj, Template)
end

0 comments on commit a2f1b84

Please sign in to comment.