Permalink
Browse files

Use a Tilt::Cache to cache rabl files sources

It's preferred to not hit the filesystem everytime you need a
template.  I used tilt cache since sinatra depends on tilt.  I
didn't mess with the gemspec in this case.
  • Loading branch information...
1 parent 483a779 commit d6309c702237b52016a8628c7526ba428504d95b @bemurphy committed May 2, 2012
Showing with 21 additions and 1 deletion.
  1. +9 −1 lib/gon/sinatra/rabl.rb
  2. +12 −0 spec/gon/gon_spec.rb
View
@@ -1,11 +1,19 @@
require 'rabl'
+require 'tilt'
module Gon
module Sinatra
module Rabl
class << self
+ def cache
+ @cache ||= Tilt::Cache.new
+ end
+
def parse_rabl(rabl_path, controller)
- source = File.read(rabl_path)
+ source = cache.fetch(rabl_path) do
+ File.read(rabl_path)
+ end
+
rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
output = rabl_engine.render(controller, {})
::Rabl.configuration.json_engine.decode(output)
View
@@ -69,6 +69,18 @@ def app
@gon.objects.length.should == 2
end
+ it 'caches the rabl template' do
+ @gon.clear
+ @objects = [1,2]
+ @gon.rabl 'spec/test_data/sample.rabl', :instance => self
+
+ File.should_not_receive(:read)
+ @gon.clear
+ @objects = [1,2,3]
+ @gon.rabl 'spec/test_data/sample.rabl', :instance => self
+ @gon.objects.length.should == 3
+ end
+
def request
@request ||= double 'request', :env => {}
end

0 comments on commit d6309c7

Please sign in to comment.