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.
18 19 20 21 |
# File 'lib/rabl-rails/library.rb', line 18 def initialize @cached_templates = ThreadSafe::Cache.new @mutex = Monitor.new end |
Instance Method Details
#compile_template_from_path(path, view) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/rabl-rails/library.rb', line 43 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
34 35 36 37 38 39 40 41 |
# File 'lib/rabl-rails/library.rb', line 34 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
27 28 29 30 31 32 |
# File 'lib/rabl-rails/library.rb', line 27 def get_rendered_template(source, view, locals = nil) compiled_template = compile_template_from_source(source, view) format = view.lookup_context.rendered_format || :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
23 24 25 |
# File 'lib/rabl-rails/library.rb', line 23 def reset_cache! @cached_templates = ThreadSafe::Cache.new end |