Class: R2Doc::TemplateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/r2doc/template.rb

Overview

The TemplateManager class handles loading and parsing of templates. The actual details of reading the file and creating a parsed template object is handled by a template engine class. TemplateManager’s responsibilities are to record registration of template directories and engines, to locate the correct template source file, and then dispatch to the right engine.

A template engine class need only supply a very limited interface. It must supply a class method named load which accepts a file name as a single argument. The load method is expected to return an instance representing that template. The returned instance must supply a result method wich accepts a binding as its single argument. The result method is expected to return the rendered template as a string. A single template instance may have its result method invoked multiple times with different bindings.

Constant Summary collapse

@@template_engines =

The collection of template engines by extension

{}
@@template_directories =

The list of template directories

[File.join(File.expand_path(File.dirname(__FILE__)), 'template')]
@@templates =

Cache for loaded templates

{}

Class Method Summary collapse

Class Method Details

.add_template_directory(dir) ⇒ Object

Add a template directory to search



43
44
45
# File 'lib/r2doc/template.rb', line 43

def self.add_template_directory(dir)
  @@template_directories.push dir
end

.file_extension(name) ⇒ Object

Determine the extension for a filename (not including the leading dot)



68
69
70
71
# File 'lib/r2doc/template.rb', line 68

def self.file_extension(name)
  m = /\.([^.]+)$/.match(name)
  m.nil? ? nil : m[1]
end

.is_registered_extension?(ext) ⇒ Boolean

Determine if a given extension is a registered extension

Returns:

  • (Boolean)


74
75
76
# File 'lib/r2doc/template.rb', line 74

def self.is_registered_extension?(ext)
  @@template_engines.has_key?(ext.nil? ? nil : ext.to_sym)
end

.load_template(name) ⇒ Object

Load a template and return its instance



58
59
60
61
62
63
64
65
# File 'lib/r2doc/template.rb', line 58

def self.load_template(name)
  return @@templates[name.to_sym] if @@templates.has_key?(name.to_sym)
  
  # find the file
  filename = self.find_template_file(name) or raise TemplateMissingError.new(name)
  fileext = self.file_extension(filename)
  @@templates[name.to_sym] = @@template_engines[fileext.to_sym].load(filename)
end

.register_engine(extension, klass) ⇒ Object

Register a template engine for an extension



38
39
40
# File 'lib/r2doc/template.rb', line 38

def self.register_engine(extension, klass)
  @@template_engines[extension.to_sym] = klass
end

.registered_extensionsObject

Return the list of registered extensions



53
54
55
# File 'lib/r2doc/template.rb', line 53

def self.registered_extensions
  @@template_engines.keys
end

.template_directoriesObject

Return the list of template directories



48
49
50
# File 'lib/r2doc/template.rb', line 48

def self.template_directories
  @@template_directories
end