Permalink
Browse files

Cleaned up Tilt[](ext) method because the redundancy was bothering me…

…. Broke out some tests to be more explicit as well.
  • Loading branch information...
1 parent 4ee7afa commit 00ce3f71b3a1954251851cafa8f6887cafc44b61 @gus gus committed Mar 18, 2010
Showing with 18 additions and 14 deletions.
  1. +9 −13 lib/tilt.rb
  2. +9 −1 test/tilt_test.rb
View
@@ -16,6 +16,10 @@ def self.register(ext, template_class)
mappings[ext.downcase] = template_class
end
+ def self.registered?(ext)
+ mappings.key?(ext.downcase)
+ end
+
# Create a new template for the given file using the file's extension
# to determine the the template mapping.
def self.new(file, line=nil, options={}, &block)
@@ -29,20 +33,12 @@ def self.new(file, line=nil, options={}, &block)
# Lookup a template class for the given filename or file
# extension. Return nil when no implementation is found.
def self.[](file)
- if @template_mappings.key?(pattern = file.to_s.downcase)
- @template_mappings[pattern]
- elsif @template_mappings.key?(pattern = File.basename(pattern))
- @template_mappings[pattern]
- else
- while !pattern.empty?
- if @template_mappings.key?(pattern)
- return @template_mappings[pattern]
- else
- pattern = pattern.sub(/^[^.]*\.?/, '')
- end
- end
- nil
+ pattern = file.to_s.downcase
+ unless registered?(pattern)
+ pattern = File.basename(pattern)
+ pattern.sub!(/^[^.]*\.?/, '') until (pattern.empty? || registered?(pattern))
end
+ @template_mappings[pattern]
end
# Mixin allowing template compilation on scope objects.
View
@@ -14,15 +14,23 @@ def initialize(*args, &block)
Tilt.register('mock', MockTemplate)
end
+ test "an extension is registered if explicit handle is found" do
+ Tilt.register('mock', MockTemplate)
+ assert Tilt.registered?('mock')
+ end
+
test "registering template classes by symbol file extension" do
Tilt.register(:mock, MockTemplate)
end
- test "looking up template classes by file extension" do
+ test "looking up template classes by exact file extension" do
Tilt.register('mock', MockTemplate)
impl = Tilt['mock']
assert_equal MockTemplate, impl
+ end
+ test "looking up template classes by implicit file extension" do
+ Tilt.register('mock', MockTemplate)
impl = Tilt['.mock']
assert_equal MockTemplate, impl
end

0 comments on commit 00ce3f7

Please sign in to comment.