Class: RablRails::Library
- Inherits:
-
Object
- Object
- RablRails::Library
- Includes:
- Singleton
- Defined in:
- lib/rabl-rails/library.rb
Constant Summary collapse
- UnknownFormat =
Class.new(StandardError)
- RENDERER_MAP =
{ json: Renderers::JSON, xml: Renderers::XML, ruby: Renderers::Hash, plist: Renderers::PLIST }.freeze
Instance Method Summary collapse
- #compile_template_from_path(path, view) ⇒ Object
- #compile_template_from_source(source, view) ⇒ Object
- #get_rendered_template(source, view, locals = nil) ⇒ Object
-
#initialize ⇒ Library
constructor
A new instance of Library.
- #reset_cache! ⇒ Object
Constructor Details
#initialize ⇒ Library
Returns a new instance of Library.
17 18 19 20 |
# File 'lib/rabl-rails/library.rb', line 17 def initialize @cached_templates = {} @monitor = Monitor.new end |
Instance Method Details
#compile_template_from_path(path, view) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/rabl-rails/library.rb', line 42 def compile_template_from_path(path, view) if RablRails.configuration.cache_templates synchronized_compile(path, nil, view) else source = fetch_source(path, view) compile(source, view) end end |
#compile_template_from_source(source, view) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/rabl-rails/library.rb', line 33 def compile_template_from_source(source, view) if RablRails.configuration.cache_templates path = view.instance_variable_get(:@virtual_path) synchronized_compile(path, source, view) else compile(source, view) end end |
#get_rendered_template(source, view, locals = nil) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/rabl-rails/library.rb', line 26 def get_rendered_template(source, view, locals = nil) compiled_template = compile_template_from_source(source, view) format = view.lookup_context.formats.first || :json raise UnknownFormat, "#{format} is not supported in rabl-rails" unless RENDERER_MAP.key?(format) RENDERER_MAP[format].render(compiled_template, view, locals) end |
#reset_cache! ⇒ Object
22 23 24 |
# File 'lib/rabl-rails/library.rb', line 22 def reset_cache! @cached_templates = {} end |