Sha256: f8cfc774b42575a2f487369c9ec18ad7964db54f58d3befb50efa4a43026c7b7

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

module Cell
  # Gets cached in production.
  class Templates
    # prefixes could be instance variable as they will never change.
    def [](prefixes, view, options)
      find_template(prefixes, view, options)
    end

  private

    def cache
      @cache ||= Cache.new
    end

    def find_template(prefixes, view, options) # options is not considered in cache key.
      cache.fetch(prefixes, view) do |prefix|
        # this block is run once per cell class per process, for each prefix/view tuple.
        create(prefix, view, options)
      end
    end

    def create(prefix, view, options)
      # puts "...checking #{prefix}/#{view}"
      return unless File.exists?("#{prefix}/#{view}") # DISCUSS: can we use Tilt.new here?

      template_class = options.delete(:template_class)
      template_class.new("#{prefix}/#{view}", options) # Tilt.new()
    end

    # {["comment/row/views", comment/views"][show.haml] => "Tpl:comment/view/show.haml"}
    class Cache
      def initialize
        @store = {}
      end

      # Iterates prefixes and yields block. Returns and caches when block returned template.
      # Note that it caches per prefixes set as this will most probably never change.
      def fetch(prefixes, view)
        template = get(prefixes, view) and return template # cache hit.

        prefixes.find do |prefix|
          template = yield(prefix) and return store(prefixes, view, template)
        end
      end

    private
      # ["comment/views"] => "show.haml"
      def get(prefixes, view)
        @store[prefixes] ||= {}
        @store[prefixes][view]
      end

      def store(prefix, view, template)
        @store[prefix][view] = template # the nested hash is always present here.
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cells-4.0.2 lib/cell/templates.rb
cells-4.0.1 lib/cell/templates.rb
cells-4.0.0 lib/cell/templates.rb
cells-4.0.0.rc1 lib/cell/templates.rb
cells-4.0.0.beta6 lib/cell/templates.rb