lib/dry/view/renderer.rb in dry-view-0.1.1 vs lib/dry/view/renderer.rb in dry-view-0.2.0

- old
+ new

@@ -2,67 +2,59 @@ require 'dry-equalizer' module Dry module View class Renderer - include Dry::Equalizer(:dir, :root, :engine) + include Dry::Equalizer(:paths, :format) TemplateNotFoundError = Class.new(StandardError) - attr_reader :dir, :root, :format, :engine, :tilts + attr_reader :paths, :format, :engine, :tilts def self.tilts @__engines__ ||= {} end - def initialize(dir, options = {}) - @dir = dir - @root = options.fetch(:root, dir) - @format = options[:format] - @engine = options[:engine] + def initialize(paths, format:) + @paths = paths + @format = format @tilts = self.class.tilts end def call(template, scope, &block) path = lookup(template) if path render(path, scope, &block) else - raise TemplateNotFoundError, "Template #{template} could not be looked up within #{root}" + msg = "Template #{template.inspect} could not be found in paths:\n#{paths.map { |pa| "- #{pa.to_s}" }.join("\n")}" + raise TemplateNotFoundError, msg end end def render(path, scope, &block) tilt(path).render(scope, &block) end - def tilt(path) - tilts.fetch(path) { tilts[path] = Tilt[engine].new(path, nil, default_encoding: "utf-8") } + def chdir(dirname) + new_paths = paths.map { |path| path.chdir(dirname) } + + self.class.new(new_paths, format: format) end def lookup(name) - template?(name) || template?("shared/#{name}") || !root? && chdir('..').lookup(name) + paths.inject(false) { |result, path| + result || path.lookup(name, format) + } end - def root? - dir == root - end + private - def template?(name) - template_path = path(name) - - if File.exist?(template_path) - template_path - end - end - - def path(name) - dir.join("#{name}.#{format}.#{engine}") - end - - def chdir(dirname) - self.class.new(dir.join(dirname), engine: engine, format: format, root: root) + # TODO: make default_encoding configurable + def tilt(path) + tilts.fetch(path) { + tilts[path] = Tilt.new(path, nil, default_encoding: "utf-8") + } end end end end