lib/tadpole/main.rb in tadpole-0.1.2 vs lib/tadpole/main.rb in tadpole-0.1.3

- old
+ new

@@ -32,65 +32,74 @@ template_paths.push(path) end def template(*path) path = absolutize_path(*path) + exists = find_matching_template_paths(path) + raise ArgumentError, "no such template `#{path}'" if exists.empty? + + create_template(*path) + end + + def create_template(*path) + path = absolutize_path(*path) name = template_mod_name(path) remove_const(name) unless caching rescue NameError return const_get(name) rescue NameError - - exists = find_matching_template_paths(path) - if exists.empty? - raise ArgumentError, "no such template `#{path}'" - end - - mod = create_template(path) + + mod = create_template_mod(path) const_set(name, mod) end - + private def absolutize_path(*path) path = path.inject([]) do |p, s| s = s.to_s; s[0, 1] == '/' ? p = [s.gsub(/^\/+/, '')] : p << s end.join(File::SEPARATOR) File.expand_path(File.join(path))[(Dir.pwd.length+1)..-1] end - def create_template(path) - mod = Module.new - mod.send(:include, Template) - mod.path = path - mod.template_paths = [] + def add_template_filters(mod) mod.before_run_filters = LocalTemplate.before_run_filters.dup mod.before_section_filters = LocalTemplate.before_section_filters.dup - + end + + def add_inherited_templates(mod, path) inherited = [] path.split(File::SEPARATOR).inject([]) do |list, el| list << el total_list = File.join(list) find_matching_template_paths(total_list).each do |subpath| - submod = create_template_mod(subpath, total_list) + submod = create_local_template_mod(subpath, total_list) mod.send :include, submod - #if total_list == path - mod.template_paths.unshift(*submod.template_paths) - mod.before_run_filters.push(*submod.before_run_filters) - mod.before_section_filters.push(*submod.before_section_filters) - inherited.push(*submod.inherited_paths) - #mod.sections = submod.sections - #end + mod.template_paths.unshift(*submod.template_paths) + mod.before_run_filters.push(*submod.before_run_filters) + mod.before_section_filters.push(*submod.before_section_filters) + inherited.push(*submod.inherited_paths) end list end mod.template_paths.push(*inherited) mod.template_paths.uniq! + end + + def create_template_mod(path) + mod = Module.new + mod.send(:include, Template) + mod.path = path + mod.template_paths = [] + + add_template_filters(mod) + add_inherited_templates(mod, path) + mod end - def create_template_mod(full_path, path) - name = 'Local'+template_mod_name(full_path) + def create_local_template_mod(full_path, path) + name = 'Local' + template_mod_name(full_path) remove_const(name) unless caching rescue NameError return const_get(name) rescue NameError mod = Module.new \ No newline at end of file