Class: TemplateMailer::TemplateFactory

Inherits:
Object
  • Object
show all
Includes:
LoggingHelper
Defined in:
lib/template_mailer/template_factory.rb

Instance Method Summary collapse

Methods included from LoggingHelper

#log

Constructor Details

#initialize(directory, logger = nil) ⇒ TemplateFactory

logger

The logger to be used by the factory. Defaults to nil.



10
11
12
13
# File 'lib/template_mailer/template_factory.rb', line 10

def initialize(directory, logger=nil)
@directory = TemplateDirectory.new(directory, logger)
@logger    = logger
end

Instance Method Details

#directoryObject

Fetches the template directory being used by the factory.



16
17
18
# File 'lib/template_mailer/template_factory.rb', line 16

def directory
	@directory.path
end

#exists?(name) ⇒ Boolean

Checks whether one or more template files exist for a given name.

Parameters

name

The name of the template to check for. A template name should be the template file name without extension(s).

Returns:

  • (Boolean)


25
26
27
# File 'lib/template_mailer/template_factory.rb', line 25

def exists?(name)
	@directory.exists?(name)
end

#manufacture(name, context = {}) ⇒ Object

This method ‘manufactures’ a set of templates based on the template name. Manufacturing involves finding all instance of a template and then using the template engine to generate the results of the template including the elements of the context passed in. The return value from this method will be a Hash of the templates generated (there may be more than one) keyed on the base file types used to generate the template instance. If a matching template cannot be found then an empty Hash is returned.

Parameters

name

The name of the template to manufacture. Template names equate to template file names minus extensions.

context

A Hash of settings that will be made available to the template engine when the template is instantiated. Defaults to {}.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/template_mailer/template_factory.rb', line 42

def manufacture(name, context={})
  paths = @directory.template_paths(name)
	log.debug "Manufacture requested for the '#{name}' template. Context:\n#{context}\nFound #{paths.size} matching template files."
	paths.inject({}) do |store, path|
		key    = file_base_type(path)
    engine = Tilt.new(path)
		log.debug "Generating template for the #{path} template file as type '#{key}'."
		store[key] = engine.render(nil, context)
		store
	end
end

#manufacture!(name, context = {}) ⇒ Object

Same as the manufacture() method except this version raises an exception if no matching templates are found.

Parameters

name

The name of the template to manufacture. Template names equate to template file names minus extensions.

context

A Hash of settings that will be made available to the template engine when the template is instantiated. Defaults to {}.



62
63
64
65
66
# File 'lib/template_mailer/template_factory.rb', line 62

def manufacture!(name, context={})
	output = manufacture(name, context)
	raise TemplateMailerError, "Unable to locate a template with the name '#{name}'." if output.empty?
	output
end