Add file?, text? and image? helpers

This commit is contained in:
Joshua Peek
2011-05-11 17:08:46 -05:00
parent 7f8d0f611e
commit b424c32bb4
2 changed files with 41 additions and 0 deletions

View File

@@ -33,6 +33,23 @@ module Linguist
@mime_type ||= Mime.lookup(extname)
end
def media_type
mime_type.split('/').first
end
def file?
image? || !text? || mime_type == 'octet-stream'
end
def text?
media_type == 'text' ||
mime_type == 'application/json'
end
def image?
['.png', '.jpg', '.jpeg', '.gif'].include?(extname)
end
def to_s
@path.dup
end

View File

@@ -57,4 +57,28 @@ class TestPathname < Test::Unit::TestCase
assert_equal 'application/python', Pathname.new("itty.py").mime_type
assert_equal 'text/plain; charset=utf-8', Pathname.new("defun.kt").mime_type
end
def test_media_type
assert_equal 'application', Pathname.new("file.js").media_type
assert_equal 'text', Pathname.new("file.txt").media_type
assert_equal 'text', Pathname.new("defun.kt").media_type
end
def test_file
assert Pathname.new("octocat.png").file?
assert Pathname.new("linguist.gem").file?
end
def test_text
assert Pathname.new("file.txt").text?
assert Pathname.new("file.json").text?
end
def test_image
assert Pathname.new("octocat.png").image?
assert Pathname.new("octocat.jpg").image?
assert Pathname.new("octocat.jpeg").image?
assert Pathname.new("octocat.gif").image?
assert !Pathname.new("octocat.psd").image?
end
end