Class: Presently::Templates

Inherits:
Object
  • Object
show all
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.expand_path("../../templates", __dir__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#rootsObject (readonly)

Returns the value of attribute roots.



35
36
37
# File 'lib/presently/templates.rb', line 35

def roots
  @roots
end


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.

Raises:

  • (Errno::ENOENT)


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

#reloadObject

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