Browse files

Mapping: Add #extensions_for

  • Loading branch information...
1 parent efa91ee commit 43108eb4e641ad759c4014a3d9bad0d2f7f671d2 @judofyr judofyr committed Jul 29, 2013
Showing with 22 additions and 0 deletions.
  1. +13 −0 lib/tilt/mapping.rb
  2. +9 −0 test/tilt_mapping_test.rb
View
13 lib/tilt/mapping.rb
@@ -176,6 +176,19 @@ def templates_for(file)
templates
end
+ # Finds the extensions the template class has been registered under.
+ # @param [template class] template_class
+ def extensions_for(template_class)
+ res = []
+ template_map.each do |ext, klass|
+ res << ext if template_class == klass
+ end
+ lazy_map.each do |ext, choices|
+ res << ext if choices.any? { |klass, file| template_class.to_s == klass }
+ end
+ res
+ end
+
private
def lazy?(ext)
View
9 test/tilt_mapping_test.rb
@@ -44,6 +44,11 @@ class Stub2
refute @mapping.registered?('baz')
end
+ test "#extensions_for" do
+ @mapping.register(Stub, 'foo', 'bar')
+ assert_equal ['foo', 'bar'].sort, @mapping.extensions_for(Stub).sort
+ end
+
test "supports old-style #register" do
@mapping.register('foo', Stub)
assert_equal Stub, @mapping['foo']
@@ -62,6 +67,10 @@ class Stub2
assert @mapping.registered?('mt')
end
+ test "#extensions_for" do
+ assert_equal ['mt'], @mapping.extensions_for('MyTemplate')
+ end
+
test "basic lookup" do
req = proc do |file|
assert_equal 'my_template', file

0 comments on commit 43108eb

Please sign in to comment.