module SitemapGenerator
# Provide convenient access to template files. E.g.
#
# SitemapGenerator.templates.sitemap_index
#
# Lazy-load and cache for efficient access.
# Define an accessor method for each template file.
class Templates
FILES = {
:sitemap_index => 'sitemap_index.builder',
:sitemap_xml => 'xml_sitemap.builder',
:sitemap_sample => 'sitemap.rb',
}
# Dynamically define accessors for each key defined in FILES
attr_accessor *FILES.keys
FILES.keys.each do |name|
eval <<-END
define_method(:#{name}) do
@#{name} ||= read_template(:#{name})
end
END
end
def initialize(root = SitemapGenerator.root)
@root = root
end
# Return the full path to a template.
#
# file template symbol e.g. :sitemap_index
def template_path(template)
File.join(@root, 'templates', self.class::FILES[template])
end
protected
# Read the template file and return its contents.
def read_template(template)
File.read(template_path(template))
end
end
end