mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Add file?, text? and image? helpers
This commit is contained in:
@@ -33,6 +33,23 @@ module Linguist
|
|||||||
@mime_type ||= Mime.lookup(extname)
|
@mime_type ||= Mime.lookup(extname)
|
||||||
end
|
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
|
def to_s
|
||||||
@path.dup
|
@path.dup
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -57,4 +57,28 @@ class TestPathname < Test::Unit::TestCase
|
|||||||
assert_equal 'application/python', Pathname.new("itty.py").mime_type
|
assert_equal 'application/python', Pathname.new("itty.py").mime_type
|
||||||
assert_equal 'text/plain; charset=utf-8', Pathname.new("defun.kt").mime_type
|
assert_equal 'text/plain; charset=utf-8', Pathname.new("defun.kt").mime_type
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user