Skip to content

Commit

Permalink
Merge pull request #2694 from github/memory_blob
Browse files Browse the repository at this point in the history
Memory blob
  • Loading branch information
arfon committed Oct 21, 2015
2 parents 5d176a7 + b3ff848 commit 64e7df7
Show file tree
Hide file tree
Showing 5 changed files with 862 additions and 532 deletions.
73 changes: 73 additions & 0 deletions lib/linguist/blob.rb
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
45 changes: 2 additions & 43 deletions lib/linguist/file_blob.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require 'linguist/blob_helper'
require 'linguist/blob'

module Linguist
# 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`.
class FileBlob
class FileBlob < Blob
include BlobHelper

# Public: Initialize a new FileBlob from a path
Expand All @@ -18,34 +19,13 @@ def initialize(path, base_path = nil)
@path = base_path ? path.sub("#{base_path}/", '') : path
end

# Public: Filename
#
# Examples
#
# FileBlob.new("/path/to/linguist/lib/linguist.rb").path
# # => "/path/to/linguist/lib/linguist.rb"
#
# FileBlob.new("/path/to/linguist/lib/linguist.rb",
# "/path/to/linguist").path
# # => "lib/linguist.rb"
#
# Returns a String
attr_reader :path

# Public: Read file permissions
#
# Returns a String like '100644'
def mode
File.stat(@fullpath).mode.to_s(8)
end

# Public: File name
#
# Returns a String
def name
File.basename(@fullpath)
end

# Public: Read file contents.
#
# Returns a String.
Expand All @@ -59,26 +39,5 @@ def data
def size
File.size(@fullpath)
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
21 changes: 17 additions & 4 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,35 @@
require "mocha/setup"
require "linguist"
require 'color-proximity'
require "linguist/blob"
require 'licensee'

def fixtures_path
File.expand_path("../fixtures", __FILE__)
end

def fixture_blob(name)
name = File.join(fixtures_path, name) unless name =~ /^\//
Linguist::FileBlob.new(name, fixtures_path)
filepath = (name =~ /^\//)? name : File.join(fixtures_path, name)
Linguist::FileBlob.new(filepath, fixtures_path)
end

def fixture_blob_memory(name)
filepath = (name =~ /^\//)? name : File.join(fixtures_path, name)
content = File.read(filepath)
Linguist::Blob.new(name, content)
end

def samples_path
File.expand_path("../../samples", __FILE__)
end

def sample_blob(name)
name = File.join(samples_path, name) unless name =~ /^\//
Linguist::FileBlob.new(name, samples_path)
filepath = (name =~ /^\//)? name : File.join(samples_path, name)
Linguist::FileBlob.new(filepath, samples_path)
end

def sample_blob_memory(name)
filepath = (name =~ /^\//)? name : File.join(samples_path, name)
content = File.read(filepath)
Linguist::Blob.new(name, content)
end
Loading

0 comments on commit 64e7df7

Please sign in to comment.