Class: Linguist::FileBlob

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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?, #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
20
21
22
23
24
25
26
# File 'lib/linguist/file_blob.rb', line 16

def initialize(path, base_path = nil)
  # Guard against unintentionally shelling out (leading '|') and path
  # traversal
  full_path = path.start_with?('/') ? path : File.join(Dir.pwd, path)
  if path.start_with?('|') || File.absolute_path(full_path) != full_path
    raise "Invalid path: #{path}"
  end

  @path = path
  @name = base_path ? path.sub("#{base_path}/", '') : path
end

Instance Attribute Details

#nameObject (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



40
41
42
# File 'lib/linguist/file_blob.rb', line 40

def name
  @name
end

Instance Method Details

#dataObject

Public: Read file contents.

Returns a String.



52
53
54
# File 'lib/linguist/file_blob.rb', line 52

def data
  File.read(@path)
end

#modeObject

Public: Read file permissions

Returns a String like ‘100644’



45
46
47
# File 'lib/linguist/file_blob.rb', line 45

def mode
  File.stat(@path).mode.to_s(8)
end

#sizeObject

Public: Get byte size

Returns an Integer.



59
60
61
# File 'lib/linguist/file_blob.rb', line 59

def size
  File.size(@path)
end