Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from bemurphy/cache_file_read

Use a Tilt::Cache to cache rabl files sources
  • Loading branch information...
commit 21e045bb97ca49500f18325e9871d901309c5ccd 2 parents 483a779 + d6309c7
@gazay authored
Showing with 21 additions and 1 deletion.
  1. +9 −1 lib/gon/sinatra/rabl.rb
  2. +12 −0 spec/gon/gon_spec.rb
View
10 lib/gon/sinatra/rabl.rb
@@ -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
12 spec/gon/gon_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.