Class: Linguist::Blob

Inherits:
Object
  • Object
show all
Includes:
BlobHelper
Defined in:
lib/linguist/blob.rb

Overview

A Blob is a wrapper around the content of a file to make it quack like a Grit::Blob. It provides the basic interface: ‘name`, `data`, `path` and `size`.

Direct Known Subclasses

FileBlob

Constant Summary

Constants included from BlobHelper

Linguist::BlobHelper::DETECTABLE_TYPES, Linguist::BlobHelper::DocumentationRegexp, Linguist::BlobHelper::MEGABYTE, Linguist::BlobHelper::VendoredRegexp

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BlobHelper

#_mime_type, #binary?, #binary_mime_type?, #content_type, #csv?, #detect_encoding, #disposition, #documentation?, #empty?, #encoded_newlines_re, #encoding, #extname, #first_lines, #generated?, #high_ratio_of_long_lines?, #image?, #include_in_language_stats?, #language, #large?, #last_lines, #likely_binary?, #lines, #loc, #mime_type, #pdf?, #ruby_encoding, #safe_to_colorize?, #sloc, #solid?, #text?, #tm_scope, #vendored?, #viewable?

Constructor Details

#initialize(path, content, symlink: false) ⇒ Blob

Public: Initialize a new Blob.

path - A path String (does not necessarily exists on the file system). content - Content of the file. symlink - Whether the file is a symlink.

Returns a Blob.



17
18
19
20
21
# File 'lib/linguist/blob.rb', line 17

def initialize(path, content, symlink: false)
  @path = path
  @content = content
  @symlink = symlink
end

Instance Attribute Details

#pathObject (readonly)

Public: Filename

Examples

Blob.new("/path/to/linguist/lib/linguist.rb", "").path
# =>  "/path/to/linguist/lib/linguist.rb"

Returns a String



31
32
33
# File 'lib/linguist/blob.rb', line 31

def path
  @path
end

Instance Method Details

#dataObject

Public: File contents.

Returns a String.



43
44
45
# File 'lib/linguist/blob.rb', line 43

def data
  @content
end

#extensionObject

Public: Get file extension.

Returns a String.



57
58
59
# File 'lib/linguist/blob.rb', line 57

def extension
  extensions.last || ""
end

#extensionsObject

Public: Return an array of the file extensions

>> Linguist::Blob.new("app/views/things/index.html.erb").extensions
=> [".html.erb", ".erb"]

Returns an Array



67
68
69
70
71
72
73
# File 'lib/linguist/blob.rb', line 67

def extensions
  _, *segments = name.downcase.split(".", -1)

  segments.map.with_index do |segment, index|
    "." + segments[index..-1].join(".")
  end
end

#nameObject

Public: File name

Returns a String



36
37
38
# File 'lib/linguist/blob.rb', line 36

def name
  File.basename(@path)
end

#sizeObject

Public: Get byte size

Returns an Integer.



50
51
52
# File 'lib/linguist/blob.rb', line 50

def size
  @content.bytesize
end

#symlink?Boolean

Public: Is this a symlink?

Returns true or false.

Returns:

  • (Boolean)


78
79
80
# File 'lib/linguist/blob.rb', line 78

def symlink?
  @symlink
end