Return nil if no Language is found

This commit is contained in:
Joshua Peek
2011-06-18 20:57:58 -05:00
parent 7c2a8874a3
commit 063bf21c66
3 changed files with 8 additions and 16 deletions

View File

@@ -256,7 +256,7 @@ module Linguist
language
# See if there is a Language for the extension
elsif language = Language.find_by_filename(pathname.to_s)
elsif language = pathname.language
language
# Try to detect Language from shebang line

View File

@@ -52,25 +52,16 @@ module Linguist
# Pathname.new('file.rb').language
# # => Language['Ruby']
#
# Returns a Langauge.
# Returns a Langauge or nil if none was found.
def language
Language.find_by_filename(@path) || Language['Text']
@language ||= Language.find_by_filename(@path)
end
# Internal: Has a language.
#
# Will return false if language was guessed to be Text.
#
# Returns true or false.
def language?
Language.find_by_filename(@path) ? true : false
end
# Deprecated: Get the lexer of the path
# Internal: Get the lexer of the path
#
# Returns a Lexer.
def lexer
language.lexer
language ? language.lexer : Lexer['Text only']
end
# Public: Get the mime type

View File

@@ -24,7 +24,7 @@ class TestPathname < Test::Unit::TestCase
end
def test_language
assert_equal Language['Text'], Pathname.new(".rb").language
assert_nil Pathname.new(".rb").language
assert_equal Language['Ruby'], Pathname.new("file.rb").language
assert_equal Language['Ruby'], Pathname.new("./file.rb").language
@@ -37,7 +37,8 @@ class TestPathname < Test::Unit::TestCase
assert_equal Language['Gentoo Ebuild'], Pathname.new("file.ebuild").language
assert_equal Language['Python'], Pathname.new("itty.py").language
assert_equal Language['Nu'], Pathname.new("itty.nu").language
assert_equal Language['Text'], Pathname.new("defun.kt").language
assert_nil Pathname.new("defun.kt").language
end
def test_lexer