class TemplateMailer::TemplateDirectory

Attributes

engines[R]
extensions[R]
pathname[R]

Public Class Methods

new(path,logger=nil) click to toggle source

Constructor for the TemplateDirectory class.

Parameters

path

The path to the template directory.

logger

The logger to be used by the directory object.

# File lib/template_mailer/template_directory.rb, line 10
def initialize(path,logger=nil)
  @pathname   = Pathname.new(path)
  @logger     = logger
  scan_templates
end

Public Instance Methods

exists?(name) click to toggle source

Checks whether at least one template file with a given name exists within the template directory.

Parameters

name

The name of the template. This should be the file name, not including base path details or extensions.

# File lib/template_mailer/template_directory.rb, line 33
def exists?(name)
    !template_paths(name).empty?
end
path() click to toggle source

Returns a String containing the template directory path.

# File lib/template_mailer/template_directory.rb, line 18
def path
  @pathname.to_s
end
template_files() click to toggle source

Returns an array of the template files within the directory.

# File lib/template_mailer/template_directory.rb, line 23
def template_files
    [].concat(@templates)
end
template_paths(name) click to toggle source

Retrieves a list of paths for all template files within a template directory that match a given template name.

Parameters

name

The name of the template. This should be the file name, not including base path details or extensions.

# File lib/template_mailer/template_directory.rb, line 43
def template_paths(name)
    @templates.inject([]) do |list, path|
            file_name = File.basename(path)
            file_name[0, name.length] == name.to_s ? list << path : list
    end
end

Private Instance Methods

scan_templates() click to toggle source

Scans the files in the template directory to generate a list of files recognised as templates based on extensions from the object itself and the engine it possesses.

# File lib/template_mailer/template_directory.rb, line 55
def scan_templates
  @templates = Dir.glob(File.join(path, "*")).inject([]) do |list, file_path|
    log.debug "Checking if #{file_path} is a recognised template file."
    if File.file?(file_path)
      file_name = File.basename(file_path)
      log.debug "#{file_path} is a template file." if !(Tilt[file_name]).nil?
      list << file_path if !(Tilt[file_name]).nil?
    end
    list
  end
end