|
1 | 1 | require 'json'
|
2 | 2 |
|
3 | 3 | module Grape
|
4 |
| - module Formatter |
5 |
| - module Rabl |
| 4 | + module Rabl |
| 5 | + class Formatter |
6 | 6 | class << self
|
7 |
| - attr_reader :env |
8 |
| - attr_reader :endpoint |
| 7 | + def tilt_cache |
| 8 | + @tilt_cache ||= ::Tilt::Cache.new |
| 9 | + end |
| 10 | + end |
9 | 11 |
|
10 |
| - def call(object, env) |
11 |
| - @env = env |
12 |
| - @endpoint = env['api.endpoint'] |
| 12 | + attr_reader :env, :endpoint, :object |
13 | 13 |
|
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 |
23 | 29 | end
|
24 |
| - else |
25 |
| - Grape::Formatter::Json.call object, env |
26 | 30 | end
|
| 31 | + else |
| 32 | + Grape::Formatter::Json.call object, env |
27 | 33 | end
|
| 34 | + end |
28 | 35 |
|
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 |
38 | 37 |
|
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')) |
41 | 43 | end
|
| 44 | + end |
42 | 45 |
|
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 |
48 | 49 |
|
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 |
52 | 55 |
|
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 |
56 | 59 |
|
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 |
60 | 63 |
|
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 |
68 | 67 |
|
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) |
71 | 73 | end
|
| 74 | + end |
72 | 75 |
|
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 |
76 | 79 |
|
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) |
84 | 86 | end
|
85 | 87 | end
|
86 | 88 | end
|
|
0 commit comments