diff options
-rw-r--r-- | lib/zero/renderer.rb | 35 | ||||
-rw-r--r-- | spec/unit/renderer/read_template_path_spec.rb | 10 |
2 files changed, 20 insertions, 25 deletions
diff --git a/lib/zero/renderer.rb b/lib/zero/renderer.rb index 473db7c..fb34d4b 100644 --- a/lib/zero/renderer.rb +++ b/lib/zero/renderer.rb @@ -56,13 +56,19 @@ module Zero # the wanted template. # @return [Self] returns the object def read_template_path! - @templates = Hash.new do |hash, key| - # TODO this is just ugly - result = [] - search_files(key).each { |file| fill_template_type_map(result, file) } - Hash[result] + # TODO clean up later + @templates = {} + search_files.each do |file| + parts = file.gsub(/#{template_path}/, '').split('.') + @templates[parts[0]] ||= {} + if parts.count > 2 then + read_type(parts[1]).each do |type| + @templates[parts[0]][type] = file + end + else + @templates[parts[0]][''] = file + end end - self end # render a template @@ -83,19 +89,8 @@ module Zero # @api private # @param template_name [String] the name of the template # @return [#each] a list of all templates found - def search_files(template_name) - Dir[template_path + template_name + '**/*.*'] - end - - # fill the `datamap` with all variants of files and types found - # @api private - # @param dataset [Hash] the hash to fill with values - # @param file [String] a filename which will be used to fill the hash - def fill_template_type_map(dataset, file) - parts = file.split('.') - read_type(parts[2]).each do |type| - dataset << [type, file] - end + def search_files + Dir[template_path + '**/*.*'] end # gets the type information from a file and converts it to an array of @@ -129,7 +124,7 @@ module Zero return Tilt.new(template) end end - raise ArgumentError "No template found for '#{name}'!" + raise ArgumentError.new "No template found for '#{name}'!" end end end diff --git a/spec/unit/renderer/read_template_path_spec.rb b/spec/unit/renderer/read_template_path_spec.rb index 8522007..bc441ed 100644 --- a/spec/unit/renderer/read_template_path_spec.rb +++ b/spec/unit/renderer/read_template_path_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Zero::Renderer, 'read_template_path!' do subject { Zero::Renderer.new(template_path, type_map) } let(:template_path) { 'foo' } - let(:file_list) { ['./foo/welcome/index.html.erb'] } + let(:file_list) { ['foo/welcome/index.html.erb'] } before :each do subject.stub(:search_files).and_return(file_list) @@ -18,14 +18,14 @@ describe Zero::Renderer, 'read_template_path!' do context 'without mapping' do let(:type_map) { {} } - let(:result) { { 'html' => './foo/welcome/index.html.erb' } } + let(:result) { { 'html' => 'foo/welcome/index.html.erb' } } it_behaves_like 'a template loader' end context 'with a single mapping' do let(:type_map) { {'html' => 'text/html' } } - let(:result) { { 'text/html' => './foo/welcome/index.html.erb' } } + let(:result) { { 'text/html' => 'foo/welcome/index.html.erb' } } it_behaves_like 'a template loader' end @@ -33,8 +33,8 @@ describe Zero::Renderer, 'read_template_path!' do context 'with multiple mappings' do let(:type_map) { {'html' => ['text/html', 'text/xml'] } } let(:result) { { - 'text/html' => './foo/welcome/index.html.erb', - 'text/xml' => './foo/welcome/index.html.erb' + 'text/html' => 'foo/welcome/index.html.erb', + 'text/xml' => 'foo/welcome/index.html.erb' } } it_behaves_like 'a template loader' |