Class: Presently::Templates
- Inherits:
-
Object
- Object
- Presently::Templates
- Defined in:
- lib/presently/templates.rb
Overview
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.
Constant Summary collapse
- DEFAULT_ROOT =
The default directory containing bundled slide templates.
File.("../../templates", __dir__)
Instance Attribute Summary collapse
-
#roots ⇒ Object
readonly
Returns the value of attribute roots.
- #The complete ordered search paths.(completeorderedsearchpaths.) ⇒ Object readonly
Class Method Summary collapse
-
.for(custom_roots = []) ⇒ Object
Create a template resolver for the given user-supplied roots, appending the built-in default.
Instance Method Summary collapse
-
#find(name) ⇒ Object
Find the path to a template by name.
-
#initialize(roots = [DEFAULT_ROOT]) ⇒ Templates
constructor
Initialize with a fully-resolved root list.
-
#reload ⇒ Object
Return a new Templates with the same roots and an empty cache.
-
#resolve(name) ⇒ Object
Resolve and load a template by name.
Constructor Details
#initialize(roots = [DEFAULT_ROOT]) ⇒ Templates
Initialize with a fully-resolved root list. Prefer build for normal use; use this when you already have the complete list.
29 30 31 32 |
# File 'lib/presently/templates.rb', line 29 def initialize(roots = [DEFAULT_ROOT]) @roots = roots @cache = {} end |
Instance Attribute Details
#roots ⇒ Object (readonly)
Returns the value of attribute roots.
35 36 37 |
# File 'lib/presently/templates.rb', line 35 def roots @roots end |
#The complete ordered search paths.(completeorderedsearchpaths.) ⇒ Object (readonly)
35 |
# File 'lib/presently/templates.rb', line 35 attr :roots |
Class Method Details
.for(custom_roots = []) ⇒ Object
Create a template resolver for the given user-supplied roots, appending the built-in default. This is the normal way to create a Presently::Templates instance.
22 23 24 |
# File 'lib/presently/templates.rb', line 22 def self.for(custom_roots = []) new(custom_roots + [DEFAULT_ROOT]) end |
Instance Method Details
#find(name) ⇒ Object
Find the path to a template by name.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/presently/templates.rb', line 58 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 |
#reload ⇒ Object
Return a new Presently::Templates with the same roots and an empty cache.
38 39 40 |
# File 'lib/presently/templates.rb', line 38 def reload self.class.new(@roots) end |
#resolve(name) ⇒ Object
Resolve and load a template by name. Searches each root directory in order for ‘name.xrb`.
47 48 49 50 51 52 |
# File 'lib/presently/templates.rb', line 47 def resolve(name) @cache[name] ||= begin path = find(name) XRB::Template.load_file(path) end end |