Class: Linguist::FileBlob
- Inherits:
-
Object
- Object
- Linguist::FileBlob
- Includes:
- BlobHelper
- Defined in:
- lib/linguist/file_blob.rb
Overview
A FileBlob is a wrapper around a File object to make it quack like a Grit::Blob. It provides the basic interface: ‘name`, `data`, and `size`.
Constant Summary
Constants included from BlobHelper
BlobHelper::MEGABYTE, BlobHelper::VendoredRegexp
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Public: Filename.
Instance Method Summary collapse
-
#data ⇒ Object
Public: Read file contents.
-
#extension ⇒ Object
Public: Get file extension.
-
#initialize(path, base_path = nil) ⇒ FileBlob
constructor
Public: Initialize a new FileBlob from a path.
-
#mode ⇒ Object
Public: Read file permissions.
-
#size ⇒ Object
Public: Get byte size.
Methods included from BlobHelper
#_mime_type, #binary?, #binary_mime_type?, #colorize, #content_type, #csv?, #detect_encoding, #disposition, #encoding, #extname, #generated?, #high_ratio_of_long_lines?, #image?, #language, #large?, #lexer, #likely_binary?, #lines, #loc, #mime_type, #pdf?, #ruby_encoding, #safe_to_colorize?, #sloc, #solid?, #text?, #vendored?, #viewable?
Constructor Details
#initialize(path, base_path = nil) ⇒ FileBlob
Public: Initialize a new FileBlob from a path
path - A path String that exists on the file system. base_path - Optional base to relativize the path
Returns a FileBlob.
16 17 18 19 |
# File 'lib/linguist/file_blob.rb', line 16 def initialize(path, base_path = nil) @path = path @name = base_path ? path.sub("#{base_path}/", '') : path end |
Instance Attribute Details
#name ⇒ Object (readonly)
Public: Filename
Examples
FileBlob.new("/path/to/linguist/lib/linguist.rb").name
# => "/path/to/linguist/lib/linguist.rb"
FileBlob.new("/path/to/linguist/lib/linguist.rb",
"/path/to/linguist").name
# => "lib/linguist.rb"
Returns a String
33 34 35 |
# File 'lib/linguist/file_blob.rb', line 33 def name @name end |
Instance Method Details
#data ⇒ Object
Public: Read file contents.
Returns a String.
45 46 47 |
# File 'lib/linguist/file_blob.rb', line 45 def data File.read(@path) end |
#extension ⇒ Object
Public: Get file extension.
Returns a String.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/linguist/file_blob.rb', line 59 def extension # File.extname returns nil if the filename is an extension. extension = File.extname(name) basename = File.basename(name) # Checks if the filename is an extension. if extension.empty? && basename[0] == "." basename else extension end end |
#mode ⇒ Object
Public: Read file permissions
Returns a String like ‘100644’
38 39 40 |
# File 'lib/linguist/file_blob.rb', line 38 def mode File.stat(@path).mode.to_s(8) end |
#size ⇒ Object
Public: Get byte size
Returns an Integer.
52 53 54 |
# File 'lib/linguist/file_blob.rb', line 52 def size File.size(@path) end |