mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Add FileBlob
This commit is contained in:
21
lib/linguist/file_blob.rb
Normal file
21
lib/linguist/file_blob.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
require 'linguist/blob_helper'
|
||||
|
||||
module Linguist
|
||||
class FileBlob
|
||||
include BlobHelper
|
||||
|
||||
def initialize(path, name = path)
|
||||
@path, @name = path, name
|
||||
end
|
||||
|
||||
attr_reader :name
|
||||
|
||||
def data
|
||||
File.read(@path)
|
||||
end
|
||||
|
||||
def size
|
||||
File.size(@path)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,23 @@
|
||||
require 'linguist/file_blob'
|
||||
|
||||
module Linguist
|
||||
class Repository
|
||||
# Public: Initialize a new Repository from a File directory
|
||||
#
|
||||
# base_path - A path String
|
||||
#
|
||||
# Returns a Repository
|
||||
def self.from_directory(base_path)
|
||||
paths = Dir["#{base_path}/**/*"].inject({}) do |h, path|
|
||||
if File.file?(path)
|
||||
name = path.sub("#{base_path}/", '')
|
||||
h[name] = FileBlob.new(path, name)
|
||||
end
|
||||
h
|
||||
end
|
||||
new(paths)
|
||||
end
|
||||
|
||||
# Public: Initialize a new Repository
|
||||
#
|
||||
# paths - A Hash of String path keys and Blob values.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
require 'linguist/blob'
|
||||
require 'linguist/file_blob'
|
||||
|
||||
require 'test/unit'
|
||||
require 'mime/types'
|
||||
@@ -6,27 +6,9 @@ require 'mime/types'
|
||||
class TestBlob < Test::Unit::TestCase
|
||||
include Linguist
|
||||
|
||||
class FixtureBlob
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@path = File.expand_path("../fixtures/blob/#{name}", __FILE__)
|
||||
end
|
||||
|
||||
def name
|
||||
@name
|
||||
end
|
||||
|
||||
def data
|
||||
File.read(@path)
|
||||
end
|
||||
|
||||
def size
|
||||
File.size(@path)
|
||||
end
|
||||
end
|
||||
|
||||
def blob(name)
|
||||
Blob.new(FixtureBlob.new(name))
|
||||
path = File.expand_path("../fixtures/blob/#{name}", __FILE__)
|
||||
FileBlob.new(path, name)
|
||||
end
|
||||
|
||||
def test_name
|
||||
|
||||
@@ -5,34 +5,8 @@ require 'test/unit'
|
||||
class TestRepository < Test::Unit::TestCase
|
||||
include Linguist
|
||||
|
||||
class FixtureBlob
|
||||
def initialize(name, path)
|
||||
@name = name
|
||||
@path = path
|
||||
end
|
||||
|
||||
def name
|
||||
@name
|
||||
end
|
||||
|
||||
def data
|
||||
File.read(@path)
|
||||
end
|
||||
|
||||
def size
|
||||
File.size(@path)
|
||||
end
|
||||
end
|
||||
|
||||
def repo(base_path)
|
||||
paths = Dir["#{base_path}/**/*"].inject({}) do |h, path|
|
||||
if File.file?(path)
|
||||
name = path.sub("#{base_path}/", '')
|
||||
h[name] = Blob.new(FixtureBlob.new(name, path))
|
||||
end
|
||||
h
|
||||
end
|
||||
Repository.new(paths)
|
||||
Repository.from_directory(base_path)
|
||||
end
|
||||
|
||||
def linguist_repo
|
||||
|
||||
Reference in New Issue
Block a user