Skip to content

Commit c5164c6

Browse files
committed
Merge pull request #38 from kushkella/thread-safety
make grape-rabl formatter thread-safe
2 parents e3f80d5 + ccfb7e8 commit c5164c6

File tree

4 files changed

+85
-68
lines changed

4 files changed

+85
-68
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#### Next
22

3+
* Make grape-rabl thread-safe. [#37](https://github.com/LTe/grape-rabl/issues/37) [@kushkella](https://github.com/kushkella)
34
* Your contribution here.
45

56
#### v0.3.1

lib/grape-rabl/formatter.rb

+64-62
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,88 @@
11
require 'json'
22

33
module Grape
4-
module Formatter
5-
module Rabl
4+
module Rabl
5+
class Formatter
66
class << self
7-
attr_reader :env
8-
attr_reader :endpoint
7+
def tilt_cache
8+
@tilt_cache ||= ::Tilt::Cache.new
9+
end
10+
end
911

10-
def call(object, env)
11-
@env = env
12-
@endpoint = env['api.endpoint']
12+
attr_reader :env, :endpoint, :object
1313

14-
if rablable?
15-
rabl do |template|
16-
engine = tilt_template(template)
17-
output = engine.render endpoint, locals
18-
if layout_template
19-
layout_template.render(endpoint) { output }
20-
else
21-
output
22-
end
14+
def initialize(object, env)
15+
@env = env
16+
@endpoint = env['api.endpoint']
17+
@object = object
18+
end
19+
20+
def render
21+
if rablable?
22+
rabl do |template|
23+
engine = tilt_template(template)
24+
output = engine.render endpoint, locals
25+
if layout_template
26+
layout_template.render(endpoint) { output }
27+
else
28+
output
2329
end
24-
else
25-
Grape::Formatter::Json.call object, env
2630
end
31+
else
32+
Grape::Formatter::Json.call object, env
2733
end
34+
end
2835

29-
private
30-
31-
def view_path(template)
32-
if template.split('.')[-1] == 'rabl'
33-
File.join(env['api.tilt.root'], template)
34-
else
35-
File.join(env['api.tilt.root'], (template + '.rabl'))
36-
end
37-
end
36+
private
3837

39-
def rablable?
40-
!!rabl_template
38+
def view_path(template)
39+
if template.split('.')[-1] == 'rabl'
40+
File.join(env['api.tilt.root'], template)
41+
else
42+
File.join(env['api.tilt.root'], (template + '.rabl'))
4143
end
44+
end
4245

43-
def rabl
44-
fail 'missing rabl template' unless rabl_template
45-
set_view_root unless env['api.tilt.root']
46-
yield rabl_template
47-
end
46+
def rablable?
47+
!!rabl_template
48+
end
4849

49-
def locals
50-
env['api.tilt.rabl_locals'] || endpoint.options[:route_options][:rabl_locals] || {}
51-
end
50+
def rabl
51+
fail 'missing rabl template' unless rabl_template
52+
set_view_root unless env['api.tilt.root']
53+
yield rabl_template
54+
end
5255

53-
def rabl_template
54-
env['api.tilt.rabl'] || endpoint.options[:route_options][:rabl]
55-
end
56+
def locals
57+
env['api.tilt.rabl_locals'] || endpoint.options[:route_options][:rabl_locals] || {}
58+
end
5659

57-
def set_view_root
58-
fail "Use Rack::Config to set 'api.tilt.root' in config.ru"
59-
end
60+
def rabl_template
61+
env['api.tilt.rabl'] || endpoint.options[:route_options][:rabl]
62+
end
6063

61-
def tilt_template(template)
62-
if Grape::Rabl.configuration.cache_template_loading
63-
tilt_cache.fetch(template) { ::Tilt.new(view_path(template), tilt_options) }
64-
else
65-
::Tilt.new(view_path(template), tilt_options)
66-
end
67-
end
64+
def set_view_root
65+
fail "Use Rack::Config to set 'api.tilt.root' in config.ru"
66+
end
6867

69-
def tilt_cache
70-
@tilt_cache ||= ::Tilt::Cache.new
68+
def tilt_template(template)
69+
if Grape::Rabl.configuration.cache_template_loading
70+
Grape::Rabl::Formatter.tilt_cache.fetch(template) { ::Tilt.new(view_path(template), tilt_options) }
71+
else
72+
::Tilt.new(view_path(template), tilt_options)
7173
end
74+
end
7275

73-
def tilt_options
74-
{ format: env['api.format'], view_path: env['api.tilt.root'] }
75-
end
76+
def tilt_options
77+
{ format: env['api.format'], view_path: env['api.tilt.root'] }
78+
end
7679

77-
def layout_template
78-
layout_path = view_path(env['api.tilt.layout'] || 'layouts/application')
79-
if Grape::Rabl.configuration.cache_template_loading
80-
tilt_cache.fetch(layout_path) { ::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path) }
81-
else
82-
::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path)
83-
end
80+
def layout_template
81+
layout_path = view_path(env['api.tilt.layout'] || 'layouts/application')
82+
if Grape::Rabl.configuration.cache_template_loading
83+
Grape::Rabl::Formatter.tilt_cache.fetch(layout_path) { ::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path) }
84+
else
85+
::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path)
8486
end
8587
end
8688
end

lib/grape-rabl/render.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
module GrapeRabl
2-
module Render
3-
def render(options = {})
4-
env['api.tilt.rabl'] = options[:rabl]
5-
env['api.tilt.rabl_locals'] = options[:locals]
1+
module Grape
2+
module Rabl
3+
module Render
4+
def render(options = {})
5+
env['api.tilt.rabl'] = options[:rabl]
6+
env['api.tilt.rabl_locals'] = options[:locals]
7+
end
68
end
79
end
810
end
911

10-
Grape::Endpoint.send(:include, GrapeRabl::Render)
12+
Grape::Endpoint.send(:include, Grape::Rabl::Render)

lib/grape/rabl.rb

+12
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
require 'grape-rabl'
2+
3+
module Grape
4+
module Formatter
5+
module Rabl
6+
class << self
7+
def call(object, env)
8+
Grape::Rabl::Formatter.new(object, env).render
9+
end
10+
end
11+
end
12+
end
13+
end

0 commit comments

Comments
 (0)