Class: SimpleJson::SimpleJsonRenderer
- Inherits:
-
Object
- Object
- SimpleJson::SimpleJsonRenderer
show all
- Includes:
- NewRelic::Agent::MethodTracer
- Defined in:
- lib/simple_json/simple_json_renderer.rb
Defined Under Namespace
Classes: TemplateNotFound
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of SimpleJsonRenderer.
57
58
59
60
|
# File 'lib/simple_json/simple_json_renderer.rb', line 57
def initialize(controller)
@controller = controller
@_assigns = controller.view_assigns.each { |key, value| instance_variable_set("@#{key}", value) }
end
|
Instance Attribute Details
#controller ⇒ Object
Returns the value of attribute controller.
7
8
9
|
# File 'lib/simple_json/simple_json_renderer.rb', line 7
def controller
@controller
end
|
Class Method Details
.clear_renderers ⇒ Object
51
52
53
54
|
# File 'lib/simple_json/simple_json_renderer.rb', line 51
def clear_renderers
@renderers = {}
@templates_loaded = false
end
|
.load_all_templates! ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/simple_json/simple_json_renderer.rb', line 16
def load_all_templates!
@renderers = {}
SimpleJson.template_paths.each do |path|
template_files = Rails.root.glob("#{path}/**/*.simple_json.rb")
template_files.each do |file_path|
template_path = file_path.relative_path_from(Rails.root.join(path)).to_path.delete_suffix('.simple_json.rb')
@renderers[template_path] = SimpleJsonTemplate.new(file_path.to_path).renderer
end
end
@templates_loaded = true
end
|
.load_template(template_path) ⇒ Object
29
30
31
32
33
34
35
36
|
# File 'lib/simple_json/simple_json_renderer.rb', line 29
def load_template(template_path)
if SimpleJson.template_cache_enabled?
load_all_templates! unless templates_loaded?
renderers[template_path]
else
load_template_from_file(template_path)
end
end
|
.load_template_from_file(template_path) ⇒ Object
38
39
40
41
42
43
44
45
|
# File 'lib/simple_json/simple_json_renderer.rb', line 38
def load_template_from_file(template_path)
SimpleJson.template_paths.each do |path|
file_path = Rails.root.join("#{path}/#{template_path}.simple_json.rb").to_path
return SimpleJsonTemplate.new(file_path).renderer if File.exist?(file_path)
end
nil
end
|
.renderers ⇒ Object
47
48
49
|
# File 'lib/simple_json/simple_json_renderer.rb', line 47
def renderers
@renderers ||= {}
end
|
.templates_loaded? ⇒ Boolean
12
13
14
|
# File 'lib/simple_json/simple_json_renderer.rb', line 12
def templates_loaded?
@templates_loaded
end
|
Instance Method Details
#cache!(key, **options, &block) ⇒ Object
86
87
88
89
90
91
92
93
|
# File 'lib/simple_json/simple_json_renderer.rb', line 86
def cache!(key, **options, &block)
if controller.perform_caching
key = Array.wrap(key).unshift(SimpleJson.cache_key_prefix)
Rails.cache.fetch(key, options, &block)
else
yield
end
end
|
#cache_if!(condition, *args, **options, &block) ⇒ Object
95
96
97
|
# File 'lib/simple_json/simple_json_renderer.rb', line 95
def cache_if!(condition, *args, **options, &block)
condition ? cache!(*args, **options, &block) : yield
end
|
#partial!(template_name, **params) ⇒ Object
80
81
82
83
84
|
# File 'lib/simple_json/simple_json_renderer.rb', line 80
def partial!(template_name, **params)
raise TemplateNotFound, "#{template_name} not found" unless renderer(template_name)
render(template_name, **params)
end
|
#render_with_tracing(template_name, params) ⇒ Object
Also known as:
render
102
103
104
105
106
|
# File 'lib/simple_json/simple_json_renderer.rb', line 102
def render_with_tracing(template_name, params)
self.class.trace_execution_scoped("View/#{template_name}.simple_json.rb/Rendering") do
render_without_tracing(template_name, params)
end
end
|
#renderer(template_path) ⇒ Object
62
63
64
65
66
|
# File 'lib/simple_json/simple_json_renderer.rb', line 62
def renderer(template_path)
renderers[template_path] || self.class.load_template(template_path).tap do |renderer|
renderers[template_path] = renderer
end
end
|
#renderers ⇒ Object
68
69
70
|
# File 'lib/simple_json/simple_json_renderer.rb', line 68
def renderers
@renderers ||= {}
end
|