0
0
Fork 0

reworked parts of the template finder

The template finder is now the instance to ask for templates. It was
already looking for them, so it should be able to handle questions
regarding the existence of templates too.
This commit is contained in:
Gibheer 2013-02-27 21:15:05 +01:00
parent d74f3da383
commit 714c540e4b
5 changed files with 140 additions and 12 deletions

View File

@ -61,6 +61,9 @@ module Zero
# @returns [Regex] the regex built from the path
attr_reader :path_regex
# returns all templates found
attr_reader :templates
# initialize a new template finder
#
# @example
@ -75,6 +78,72 @@ module Zero
@path = path
@type_map = sanity_map(type_map)
@path_regex = /#{path}/
@templates = load_templates
end
# get the template
#
# This function returns the template when found.
# @raise ArgumentError when the template was not found the type
# @param template [String] the template to return
# @param type [String] the type to return the template for
# @return [Tilt::Template] the tilt template
def get(template, types)
raise ArgumentError.new(<<-ERROR) unless exist?(template)
Template '#{template}' does not exist!
ERROR
types.each do |type|
return Tilt.new(get_template(template, type)) if has_template?(template, type)
end
raise ArgumentError.new(<<-ERROR)
Template '#{template}' not found!
types: #{types.inspect}
ERROR
end
# check if a specific template exists
#
# This function checks for the existance of the specifiec template.
# @param template [String] the template to find
# @returns [Boolean] true when template was found
def exist?(template)
templates.has_key?(template)
end
# check if the template exists for the specified types
#
# This function takes the template and searches for any template for the
# types.
# @param template [String] the template to look for
# @param types [Array<String>] the types to test
# @return [Boolean] true when a template is found, else false
def exist_for_types?(template, types)
return false unless exist?(template)
types.each do |type|
return has_template?(template, type)
end
false
end
private
# checks if the template has support for the type
#
# @param template [String] the template to look for
# @param type [String] the type to look for
# @return [Boolean] true when the template was found
def has_template?(template, type)
templates[template].has_key?(type)
end
# returns the template for the type
#
# This function returns the template for the specified type.
# @param template [String] the template to return
# @param type [String] the type to return the template for
# @return [String] the filename for the template
def get_template(template, type)
templates[template][type]
end
# traverses the template path to gather all templates
@ -82,18 +151,17 @@ module Zero
# This function traverses the template path, collects and sorts all
# templates into the target types given at initialization.
# @return [Hash] the map of type to template
def get_templates
result = Hash.new {|hash, key| hash[key] = {} }
def load_templates
result = {}
search_files.each do |file|
key, value = add_template(file)
result[key] = {} unless result.has_key?(key)
result[key] = result[key].merge(value)
end
result
end
private
# returns a list of files found at @path
#
# This method returns all files found in @path, which look like a template.

View File

@ -0,0 +1,25 @@
require 'spec_helper'
describe Zero::Renderer::TemplateFinder, '#exist_for_types?' do
subject { described_class.new(template_path, type_map) }
let(:template_path) { 'spec/fixtures/templates/' }
let(:type_map) { {'html' => 'text/html'} }
let(:html_types) { ['text/html'] }
let(:foo_types) { ['foo/bar', 'bar/foo'] }
it 'returns true when template exists' do
expect(subject.exist_for_types?('index', html_types)).to be(true)
end
it 'returns false when template does not exist' do
expect(subject.exist_for_types?('not_found', html_types)).to be(false)
end
it 'returns false when template for types does not exists' do
expect(subject.exist_for_types?('index', foo_types)).to be(false)
end
it 'returns false when no types are defined' do
expect(subject.exist_for_types?('index', [])).to be(false)
end
end

View File

@ -0,0 +1,15 @@
require 'spec_helper'
describe Zero::Renderer::TemplateFinder, '#exist?' do
subject { described_class.new(template_path, type_map) }
let(:template_path) { 'spec/fixtures/templates/' }
let(:type_map) { {'html' => 'text/html'} }
it 'returns true when the template exists' do
expect(subject.exist?('index')).to be(true)
end
it 'returns false when the template does not exist' do
expect(subject.exist?('not_found')).to be(false)
end
end

View File

@ -0,0 +1,27 @@
require 'spec_helper'
describe Zero::Renderer::TemplateFinder, '#get' do
subject { described_class.new(template_path, type_map) }
let(:template_path) { 'spec/fixtures/templates/' }
let(:type_map) {{
'html' => ['text/html', 'text/xml', '*/*'],
'json' => ['application/json', 'plain/text']
}}
let(:html_types) { ['text/html'] }
let(:json_types) { ['application/json'] }
let(:foo_types) { ['foo/bar', 'bar/foo'] }
it 'returns a Tilt template' do
expect(subject.get('index', html_types)).to be_kind_of(Tilt::Template)
end
it 'raises an Error when the template is not found' do
expect { subject.get('not_exist', html_types) }.to raise_error(
ArgumentError, /Template 'not_exist' does not exist/)
end
it 'raises an Error when no type for the template was found' do
expect { subject.get('index', foo_types) }.to raise_error(
ArgumentError, /Template 'index' not found/)
end
end

View File

@ -17,7 +17,7 @@ describe Zero::Renderer::TemplateFinder, '#initialize' do
shared_examples_for 'a template loader' do
it 'creates a template tree' do
subject.get_templates['welcome/index'].should eq(result)
subject.templates['welcome/index'].should eq(result)
end
end
@ -55,11 +55,4 @@ describe Zero::Renderer::TemplateFinder, '#initialize' do
it_behaves_like 'a template loader'
end
it 'creates an empty templates list without templates in path' do
subject = Zero::Renderer.new("bar/", {})
subject.read_template_path!
subject.templates.should eq({})
end
end