diff options
| -rw-r--r-- | lib/zero/renderer/template_finder.rb | 76 | ||||
| -rw-r--r-- | spec/unit/zero/renderer/template_finder/exist_for_types_predicate_spec.rb | 25 | ||||
| -rw-r--r-- | spec/unit/zero/renderer/template_finder/exist_predicate_spec.rb | 15 | ||||
| -rw-r--r-- | spec/unit/zero/renderer/template_finder/get_spec.rb | 27 | ||||
| -rw-r--r-- | spec/unit/zero/renderer/template_finder/templates_spec.rb (renamed from spec/unit/zero/renderer/template_finder/get_templates_spec.rb) | 9 | 
5 files changed, 140 insertions, 12 deletions
| diff --git a/lib/zero/renderer/template_finder.rb b/lib/zero/renderer/template_finder.rb index 39fb8cb..d52a147 100644 --- a/lib/zero/renderer/template_finder.rb +++ b/lib/zero/renderer/template_finder.rb @@ -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. diff --git a/spec/unit/zero/renderer/template_finder/exist_for_types_predicate_spec.rb b/spec/unit/zero/renderer/template_finder/exist_for_types_predicate_spec.rb new file mode 100644 index 0000000..794a4c9 --- /dev/null +++ b/spec/unit/zero/renderer/template_finder/exist_for_types_predicate_spec.rb @@ -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 diff --git a/spec/unit/zero/renderer/template_finder/exist_predicate_spec.rb b/spec/unit/zero/renderer/template_finder/exist_predicate_spec.rb new file mode 100644 index 0000000..0444622 --- /dev/null +++ b/spec/unit/zero/renderer/template_finder/exist_predicate_spec.rb @@ -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 diff --git a/spec/unit/zero/renderer/template_finder/get_spec.rb b/spec/unit/zero/renderer/template_finder/get_spec.rb new file mode 100644 index 0000000..d4d40e9 --- /dev/null +++ b/spec/unit/zero/renderer/template_finder/get_spec.rb @@ -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 diff --git a/spec/unit/zero/renderer/template_finder/get_templates_spec.rb b/spec/unit/zero/renderer/template_finder/templates_spec.rb index 5928e66..e6c0edd 100644 --- a/spec/unit/zero/renderer/template_finder/get_templates_spec.rb +++ b/spec/unit/zero/renderer/template_finder/templates_spec.rb @@ -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 | 
