Sha256: 22853ba5496f83d3e21604c3ec48ab3a54c2f91e2e9818ca153a05810d3b5489
Contents?: true
Size: 1.74 KB
Versions: 1
Compression:
Stored size: 1.74 KB
Contents
module Cell # Gets cached in production. class Templates # prefixes could be instance variable as they will never change. def [](bases, prefixes, view, engine, formats=nil) base = bases.first # FIXME. find_template(base, prefixes, view, engine) end private def cache @cache ||= Cache.new end def find_template(base, prefixes, view, engine) view = "#{view}.#{engine}" cache.fetch(prefixes, view) do |prefix| # this block is run once per cell class per process, for each prefix/view tuple. create(base, prefix, view) end end def create(base, prefix, view) puts "checking #{base}/#{prefix}/#{view}" return unless File.exists?("#{base}/#{prefix}/#{view}") # DISCUSS: can we use Tilt.new here? Tilt.new("#{base}/#{prefix}/#{view}", :escape_html => false, :escape_attrs => false) 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
cells-jamie-4.0.0.alpha1 | lib/cell/templates.rb |