-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2694 from github/memory_blob
Memory blob
- Loading branch information
Showing
5 changed files
with
862 additions
and
532 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require 'linguist/blob_helper' | ||
|
||
module Linguist | ||
# 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`. | ||
class Blob | ||
include BlobHelper | ||
|
||
# Public: Initialize a new Blob. | ||
# | ||
# path - A path String (does not necessarily exists on the file system). | ||
# content - Content of the file. | ||
# | ||
# Returns a FileBlob. | ||
def initialize(path, content) | ||
@path = path | ||
@content = content | ||
end | ||
|
||
# Public: Filename | ||
# | ||
# Examples | ||
# | ||
# Blob.new("/path/to/linguist/lib/linguist.rb", "").path | ||
# # => "/path/to/linguist/lib/linguist.rb" | ||
# | ||
# Returns a String | ||
attr_reader :path | ||
|
||
# Public: File name | ||
# | ||
# Returns a String | ||
def name | ||
File.basename(@path) | ||
end | ||
|
||
# Public: File contents. | ||
# | ||
# Returns a String. | ||
def data | ||
@content | ||
end | ||
|
||
# Public: Get byte size | ||
# | ||
# Returns an Integer. | ||
def size | ||
@content.bytesize | ||
end | ||
|
||
# Public: Get file extension. | ||
# | ||
# Returns a String. | ||
def extension | ||
extensions.last || "" | ||
end | ||
|
||
# Public: Return an array of the file extensions | ||
# | ||
# >> Linguist::FileBlob.new("app/views/things/index.html.erb").extensions | ||
# => [".html.erb", ".erb"] | ||
# | ||
# Returns an Array | ||
def extensions | ||
basename, *segments = name.downcase.split(".") | ||
|
||
segments.map.with_index do |segment, index| | ||
"." + segments[index..-1].join(".") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.