From b424c32bb4f29e156d38d0828533f0d2d208bc22 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 11 May 2011 17:08:46 -0500 Subject: [PATCH] Add file?, text? and image? helpers --- lib/linguist/pathname.rb | 17 +++++++++++++++++ test/test_pathname.rb | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/linguist/pathname.rb b/lib/linguist/pathname.rb index 4dabed20..0b5dab76 100644 --- a/lib/linguist/pathname.rb +++ b/lib/linguist/pathname.rb @@ -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 diff --git a/test/test_pathname.rb b/test/test_pathname.rb index 66aaa80b..30817c15 100644 --- a/test/test_pathname.rb +++ b/test/test_pathname.rb @@ -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