Sha256: 9029159f6a6204ab61817bcc287aecc78ca8f5e710f58f91c7d5d210cddd1205

Contents?: true

Size: 1.9 KB

Versions: 16

Compression:

Stored size: 1.9 KB

Contents

require 'linguist/blob_helper'

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
    include BlobHelper

    # 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.
    def initialize(path, base_path = nil)
      @fullpath = path
      @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.
    def data
      File.read(@fullpath)
    end

    # Public: Get byte size
    #
    # Returns an Integer.
    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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
github-linguist-4.6.4 lib/linguist/file_blob.rb
github-linguist-4.6.3 lib/linguist/file_blob.rb
github-linguist-4.6.0 lib/linguist/file_blob.rb
github-linguist-4.5.15 lib/linguist/file_blob.rb
github-linguist-4.5.14 lib/linguist/file_blob.rb
github-linguist-4.5.13 lib/linguist/file_blob.rb
github-linguist-4.5.11 lib/linguist/file_blob.rb
github-linguist-4.5.10 lib/linguist/file_blob.rb
github-linguist-4.5.9 lib/linguist/file_blob.rb
github-linguist-4.5.8 lib/linguist/file_blob.rb
github-linguist-4.5.6 lib/linguist/file_blob.rb
github-linguist-4.5.5 lib/linguist/file_blob.rb
github-linguist-4.5.4 lib/linguist/file_blob.rb
github-linguist-4.5.3 lib/linguist/file_blob.rb
github-linguist-4.5.2 lib/linguist/file_blob.rb
github-linguist-4.5.1 lib/linguist/file_blob.rb