lib/mattock/template-host.rb in mattock-0.3.4 vs lib/mattock/template-host.rb in mattock-0.4.0

- old
+ new

@@ -2,83 +2,61 @@ require 'valise' module Mattock module ValiseManager def default_valise(*dirs) - Valise::Set.define do - dirs.each do |dir| - ro dir - end - end + Valise::read_only(*dirs) end def rel_dir(base_path = nil, up_to = nil) - base_path ||= /(.*):\d+/.match(caller[0])[1] - up_to ||= "lib" - - abs_path = File::expand_path(base_path) - base_path = abs_path.split(File::Separator) - until base_path.empty? - unless base_path.last == up_to - base_path.pop - else - break - end - end - - if base_path.empty? - raise "Relative root #{up_to.inspect} not found in #{abs_path.inspect}" - end - - return base_path + Valise::Unpath.up_to(up_to, base_path) end module_function :rel_dir, :default_valise public :rel_dir, :default_valise end module TemplateHost - def self.template_cache - @template_cache ||= Tilt::Cache.new - end - attr_accessor :valise + def templates_are_in(valise) + self.valise = valise.templates + end + + #XXX Better to let clients stem or subset def find_template(path) - valise.find(["templates"] + valise.unpath(path)) + valise.find(path) end - def template_contents(path) + def template(path) find_template(path).contents end def template_path(path) find_template(path).full_path end - def render(path, template_options = nil) - template = TemplateHost::template_cache.fetch(path) do - Tilt.new(path, template_options || {}) do |tmpl| - template_contents(tmpl.file) - end - end - + def render(path) locals = {} if block_given? yield locals end - template.render(self, locals) + template(path).render(self, locals) end end module TemplateTaskLib include TemplateHost def template_task(template_source, destination_path, template_options = nil) + unless template_options.nil? + valise.add_serialization_handler(template_source, :tilt, :template_options => template_options) + end + file template_path(template_source) file destination_path => template_path(template_source) do File::open(destination_path, "w") do |file| - file.write(render(template_source, template_options)) + file.write(render(template_source)) end end end end end