Skip to content

Commit 79b1e58

Browse files
author
Kush Kella
committed
added cache template loading support
1 parent 3a3765b commit 79b1e58

File tree

6 files changed

+141
-4
lines changed

6 files changed

+141
-4
lines changed

lib/grape-rabl.rb

+20
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,23 @@
55
require 'grape-rabl/version'
66
require 'grape-rabl/formatter'
77
require 'grape-rabl/render'
8+
require 'grape-rabl/configuration'
9+
10+
module Grape
11+
module Rabl
12+
class << self
13+
def configure(&block)
14+
yield(configuration)
15+
configuration
16+
end
17+
18+
def configuration
19+
@configuration ||= Configuration.new
20+
end
21+
22+
def reset_configuration!
23+
@configuration = nil
24+
end
25+
end
26+
end
27+
end

lib/grape-rabl/configuration.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Grape
2+
module Rabl
3+
class Configuration
4+
attr_accessor :cache_template_loading
5+
6+
def initialize
7+
@cache_template_loading = false
8+
end
9+
end
10+
end
11+
end

lib/grape-rabl/formatter.rb

+16-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def call(object, env)
1313

1414
if rablable?
1515
rabl do |template|
16-
engine = ::Tilt.new(view_path(template), tilt_options)
16+
engine = tilt_template(template)
1717
output = engine.render endpoint, locals
1818
if layout_template
1919
layout_template.render(endpoint) { output }
@@ -55,16 +55,28 @@ def set_view_root
5555
fail "Use Rack::Config to set 'api.tilt.root' in config.ru"
5656
end
5757

58+
def tilt_template(template)
59+
if Grape::Rabl.configuration.cache_template_loading
60+
tilt_cache.fetch(template) { ::Tilt.new(view_path(template), tilt_options) }
61+
else
62+
::Tilt.new(view_path(template), tilt_options)
63+
end
64+
end
65+
66+
def tilt_cache
67+
@tilt_cache ||= ::Tilt::Cache.new
68+
end
69+
5870
def tilt_options
5971
{ format: env['api.format'], view_path: env['api.tilt.root'] }
6072
end
6173

6274
def layout_template
6375
layout_path = view_path(env['api.tilt.layout'] || 'layouts/application')
64-
if File.exist?(layout_path)
65-
::Tilt.new(layout_path, tilt_options)
76+
if Grape::Rabl.configuration.cache_template_loading
77+
tilt_cache.fetch(layout_path) { ::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path) }
6678
else
67-
nil
79+
::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path)
6880
end
6981
end
7082
end

spec/grape_rabl_configuration.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'spec_helper'
2+
3+
describe 'Grape::Rabl configuration' do
4+
context 'configuration' do
5+
it 'should return default values' do
6+
Grape::Rabl.configuration.cache_template_loading.should == false
7+
end
8+
9+
it 'should set and reset configuration' do
10+
Grape::Rabl.configure do |config|
11+
config.cache_template_loading = true
12+
end
13+
Grape::Rabl.configuration.cache_template_loading.should == true
14+
Grape::Rabl.reset_configuration!
15+
Grape::Rabl.configuration.cache_template_loading.should == false
16+
end
17+
end
18+
end

spec/grape_rabl_layout_spec.rb

+39
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,43 @@ def app
4949
%Q({"result":{"user":{"name":"LTe","project":{"name":"First"}}}})
5050
end
5151
end
52+
53+
context 'layout cache' do
54+
before do
55+
@layout = "#{File.dirname(__FILE__)}/views/layout_test/layouts/application_cached.rabl"
56+
FileUtils.cp("#{File.dirname(__FILE__)}/views/layout_test/layouts/application.rabl", @layout)
57+
subject.before { env['api.tilt.layout'] = 'layouts/application_cached' }
58+
subject.get('/home', rabl: 'user') do
59+
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
60+
@project = OpenStruct.new(name: 'First')
61+
@status = 200
62+
end
63+
end
64+
65+
after do
66+
Grape::Rabl.reset_configuration!
67+
FileUtils.rm(@layout)
68+
end
69+
70+
it 'should serve from cache if cache_template_loading' do
71+
Grape::Rabl.configure do |config|
72+
config.cache_template_loading = true
73+
end
74+
get '/home'
75+
old_response = last_response.body
76+
open(@layout, 'a') { |f| f << 'node(:test) { "test" }' }
77+
get '/home'
78+
new_response = last_response.body
79+
old_response.should == new_response
80+
end
81+
82+
it 'should serve new template if cache_template_loading' do
83+
get '/home'
84+
old_response = last_response.body
85+
open(@layout, 'a') { |f| f << 'node(:test) { "test" }' }
86+
get '/home'
87+
new_response = last_response.body
88+
old_response.should_not == new_response
89+
end
90+
end
5291
end

spec/grape_rabl_spec.rb

+37
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,42 @@ def app
111111
last_response.body.should == '{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}'
112112
end
113113
end
114+
115+
describe 'template cache' do
116+
before do
117+
@template = "#{File.dirname(__FILE__)}/views/user_cached.rabl"
118+
FileUtils.cp("#{File.dirname(__FILE__)}/views/user.rabl", @template)
119+
subject.get('/home', rabl: 'user_cached') do
120+
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
121+
@project = OpenStruct.new(name: 'First')
122+
end
123+
end
124+
125+
after do
126+
Grape::Rabl.reset_configuration!
127+
FileUtils.rm(@template)
128+
end
129+
130+
it 'should serve from cache if cache_template_loading' do
131+
Grape::Rabl.configure do |config|
132+
config.cache_template_loading = true
133+
end
134+
get '/home'
135+
old_response = last_response.body
136+
open(@template, 'a') { |f| f << 'node(:test) { "test" }' }
137+
get '/home'
138+
new_response = last_response.body
139+
old_response.should == new_response
140+
end
141+
142+
it 'should serve new template if cache_template_loading' do
143+
get '/home'
144+
old_response = last_response.body
145+
open(@template, 'a') { |f| f << 'node(:test) { "test" }' }
146+
get '/home'
147+
new_response = last_response.body
148+
old_response.should_not == new_response
149+
end
150+
end
114151
end
115152
end

0 commit comments

Comments
 (0)