class Templates
Resolves and caches XRB templates using a layered search path.
Templates are looked up by name across multiple root directories in order. The first match wins, allowing user templates to override gem defaults without duplicating the entire set.
Definitions
DEFAULT_ROOT = File.expand_path("../../templates", __dir__)
The default directory containing bundled slide templates.
def self.for(custom_roots = [])
Create a template resolver for the given user-supplied roots, appending the built-in default.
This is the normal way to create a class Presently::Templates instance.
Signature
-
parameter
custom_rootsArray(String) User-supplied directories to search first.
-
returns
Templates
Implementation
def self.for(custom_roots = [])
new(custom_roots + [DEFAULT_ROOT])
end
def initialize(roots = [DEFAULT_ROOT])
Initialize with a fully-resolved root list.
Prefer .build for normal use; use this when you already have the complete list.
Signature
-
parameter
rootsArray(String) The complete ordered list of directories to search.
Implementation
def initialize(roots = [DEFAULT_ROOT])
@roots = roots
@cache = {}
end
attr :roots
Signature
-
attribute
Array(String) The complete ordered search paths.
def reload
Return a new class Presently::Templates with the same roots and an empty cache.
Implementation
def reload
self.class.new(@roots)
end
def resolve(name)
Resolve and load a template by name.
Searches each root directory in order for {name}.xrb.
Signature
-
parameter
nameString The template name (without extension).
-
returns
XRB::Template The loaded template.
-
raises
Errno::ENOENT If the template is not found in any root.
Implementation
def resolve(name)
@cache[name] ||= begin
path = find(name)
XRB::Template.load_file(path)
end
end
def find(name)
Find the path to a template by name.
Signature
-
parameter
nameString The template name (without extension).
-
returns
String The absolute path to the template file.
-
raises
Errno::ENOENT If the template is not found in any root.
Implementation
def find(name)
filename = "#{name}.xrb"
@roots.each do |root|
path = File.join(root, filename)
return path if File.exist?(path)
end
raise Errno::ENOENT, "Template '#{name}' not found in: #{@roots.join(', ')}"
end