Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a Tilt::Cache to cache rabl files sources #3

Merged
merged 1 commit into from May 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/gon/sinatra/rabl.rb
@@ -1,11 +1,19 @@
require 'rabl' require 'rabl'
require 'tilt'


module Gon module Gon
module Sinatra module Sinatra
module Rabl module Rabl
class << self class << self
def cache
@cache ||= Tilt::Cache.new
end

def parse_rabl(rabl_path, controller) 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') rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
output = rabl_engine.render(controller, {}) output = rabl_engine.render(controller, {})
::Rabl.configuration.json_engine.decode(output) ::Rabl.configuration.json_engine.decode(output)
Expand Down
12 changes: 12 additions & 0 deletions spec/gon/gon_spec.rb
Expand Up @@ -69,6 +69,18 @@ def app
@gon.objects.length.should == 2 @gon.objects.length.should == 2
end 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 def request
@request ||= double 'request', :env => {} @request ||= double 'request', :env => {}
end end
Expand Down