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`, `path` and `size`.
Constant Summary
Constants included from BlobHelper
BlobHelper::DETECTABLE_TYPES, BlobHelper::DocumentationRegexp, BlobHelper::MEGABYTE, BlobHelper::VendoredRegexp
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Public: Filename.
Instance Method Summary collapse
-
#data ⇒ Object
Public: Read file contents.
-
#extension ⇒ Object
Public: Get file extension.
-
#extensions ⇒ Object
Public: Return an array of the file extensions.
-
#initialize(path, base_path = nil) ⇒ FileBlob
constructor
Public: Initialize a new FileBlob from a path.
-
#mode ⇒ Object
Public: Read file permissions.
-
#name ⇒ Object
Public: File name.
-
#size ⇒ Object
Public: Get byte size.
Methods included from BlobHelper
#_mime_type, #binary?, #binary_mime_type?, #content_type, #csv?, #detect_encoding, #disposition, #documentation?, #empty?, #encoding, #extname, #generated?, #high_ratio_of_long_lines?, #image?, #include_in_language_stats?, #language, #large?, #likely_binary?, #lines, #loc, #mime_type, #pdf?, #ruby_encoding, #safe_to_colorize?, #sloc, #solid?, #text?, #tm_scope, #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) @fullpath = path @path = base_path ? path.sub("#{base_path}/", '') : path end |
Instance Attribute Details
#path ⇒ Object (readonly)
33 34 35 |
# File 'lib/linguist/file_blob.rb', line 33 def path @path end |
Instance Method Details
#data ⇒ Object
Public: Read file contents.
Returns a String.
52 53 54 |
# File 'lib/linguist/file_blob.rb', line 52 def data File.read(@fullpath) end |
#extension ⇒ Object
Public: Get file extension.
Returns a String.
66 67 68 |
# File 'lib/linguist/file_blob.rb', line 66 def extension extensions.last || "" end |
#extensions ⇒ Object
Public: Return an array of the file extensions
>> Linguist::FileBlob.new("app/views/things/index.html.erb").extensions
=> [".html.erb", ".erb"]
Returns an Array
76 77 78 79 80 81 82 |
# File 'lib/linguist/file_blob.rb', line 76 def extensions basename, *segments = name.downcase.split(".") segments.map.with_index do |segment, index| "." + segments[index..-1].join(".") 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(@fullpath).mode.to_s(8) end |
#name ⇒ Object
Public: File name
Returns a String
45 46 47 |
# File 'lib/linguist/file_blob.rb', line 45 def name File.basename(@fullpath) end |
#size ⇒ Object
Public: Get byte size
Returns an Integer.
59 60 61 |
# File 'lib/linguist/file_blob.rb', line 59 def size File.size(@fullpath) end |