public
Fork of mojombo/grit
Description: Grit is a Ruby library for extracting information from a git repository in an object oriented manner.
Homepage: http://grit.rubyforge.org/
Clone URL: git://github.com/technoweenie/grit.git
add Commit#mime_type
mojombo (author)
Sun Jan 06 18:59:51 -0800 2008
commit  3e0955045cb189a7112015c26132152a94f637bf
tree    54f2aa4afa386748e129d209103b1235e8de4aaa
parent  b86b48e3520a106739035b149dbed97445152868
...
4
5
6
 
 
 
 
7
8
9
...
4
5
6
7
8
9
10
11
12
13
0
@@ -4,6 +4,10 @@ $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
0
 
0
 # stdlib
0
 
0
+# third party
0
+require 'rubygems'
0
+require 'mime/types'
0
+
0
 # internal requires
0
 require 'grit/lazy'
0
 require 'grit/errors'
...
1
2
3
 
 
4
5
6
...
41
42
43
 
 
 
 
 
 
 
 
44
45
46
...
1
2
3
4
5
6
7
8
...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
0
@@ -1,6 +1,8 @@
0
 module Grit
0
   
0
   class Blob
0
+ DEFAULT_MIME_TYPE = "text/plain"
0
+
0
     attr_reader :id
0
     attr_reader :mode
0
     attr_reader :name
0
@@ -41,6 +43,14 @@ module Grit
0
       @data ||= @repo.git.cat_file({:p => true}, id)
0
     end
0
     
0
+ # The mime type of this file (based on the filename)
0
+ #
0
+ # Returns String
0
+ def mime_type
0
+ guesses = MIME::Types.type_for(self.name) rescue []
0
+ guesses.first ? guesses.first.simplified : DEFAULT_MIME_TYPE
0
+ end
0
+
0
     # The blame information for the given file at the given commit
0
     #
0
     # Returns Array: [Grit::Commit, Array: [<line>]]
...
29
30
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
33
34
...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -29,6 +29,20 @@ class TestBlob < Test::Unit::TestCase
0
     assert_equal 11, blob.size
0
   end
0
   
0
+ # data
0
+
0
+ # mime_type
0
+
0
+ def test_mime_type_should_return_mime_type_for_known_types
0
+ blob = Blob.create(@r, :id => 'abc', :name => 'foo.png')
0
+ assert_equal "image/png", blob.mime_type
0
+ end
0
+
0
+ def test_mime_type_should_return_text_plain_for_unknown_types
0
+ blob = Blob.create(@r, :id => 'abc')
0
+ assert_equal "text/plain", blob.mime_type
0
+ end
0
+
0
   # blame
0
   
0
   def test_blame

Comments

    No one has commented yet.