File: library.rb

package info (click to toggle)
ruby-rabl-rails 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 288 kB
  • sloc: ruby: 1,630; makefile: 4
file content (75 lines) | stat: -rw-r--r-- 2,049 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'singleton'
require 'monitor'

module RablRails
  class Library
    include Singleton

    UnknownFormat = Class.new(StandardError)

    RENDERER_MAP = {
      json:   Renderers::JSON,
      xml:    Renderers::XML,
      ruby:   Renderers::Hash,
      plist:  Renderers::PLIST
    }.freeze

    def initialize
      @cached_templates = {}
      @monitor = Monitor.new
    end

    def reset_cache!
      @cached_templates = {}
    end

    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

    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

    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

    private

    def synchronized_compile(path, source, view)
      @cached_templates[path] || @monitor.synchronize do
        # Any thread holding this lock will be compiling the template needed
        # by the threads waiting. So re-check the template presence to avoid
        # re-compilation
        @cached_templates.fetch(path) do
          source ||= fetch_source(path, view)
          @cached_templates[path] = compile(source, view)
        end
      end
    end

    def compile(source, view)
      Compiler.new(view).compile_source(source)
    end

    def fetch_source(path, view)
      t = view.lookup_context.find_template(path, [], false)
      t = t.refresh(view) unless t.source
      t.source
    end
  end
end