class Kadim::MemoryResolver

Public Class Methods

new() click to toggle source
# File lib/kadim/template/memory_resolver.rb, line 39
def initialize
  @cache = Cache.new
  @store = TemplateStore.new
end

Public Instance Methods

add(body, path) click to toggle source
# File lib/kadim/template/memory_resolver.rb, line 44
def add(body, path)
  partial = path.split("/").last.start_with?("_")
  @store.add(body, path, partial)
end
clear() click to toggle source
# File lib/kadim/template/memory_resolver.rb, line 49
def clear
  @cache.clear
  @store.clear
end

Private Instance Methods

find_templates(name, prefix, partial, details, locals = []) click to toggle source
# File lib/kadim/template/memory_resolver.rb, line 55
def find_templates(name, prefix, partial, details, locals = [])
  details[:handlers].map! { |handler| handler.to_sym }
  return unless details[:formats].include?(:html) && details[:handlers].include?(:erb)

  body = @store.find(name, prefix, partial)
  return body if body.empty?

  path = normalize_path(name, prefix)
  identifier = "#{virtual_path(path, partial)}.html.erb (kadim memory resolver)"
  handler = ActionView::Template.registered_template_handler(:erb)
  details = { format: :html, virtual_path: virtual_path(path, partial), locals: locals }

  [ActionView::Template.new(body, identifier, handler, details)]
end
normalize_path(name, prefix) click to toggle source

Normalize name and prefix, so the tuple [“index”, “users”] becomes “users/index” and the tuple

“template”, nil

becomes “template”.

# File lib/kadim/template/memory_resolver.rb, line 72
def normalize_path(name, prefix)
  prefix.present? ? "#{prefix}/#{name}" : name
end
virtual_path(path, partial) click to toggle source

Make paths as “users/user” become “users/_user” for partials.

# File lib/kadim/template/memory_resolver.rb, line 77
def virtual_path(path, partial)
  return path unless partial

  if (index = path.rindex("/"))
    path.dup.insert(index + 1, "_")
  else
    "_#{path}"
  end
end