Sha256: 15fde41febc81ed7cd53fdb189f683b5eab24b1aea70e2d96f37c649f9ab06e0

Contents?: true

Size: 1.52 KB

Versions: 7

Compression:

Stored size: 1.52 KB

Contents

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 Blob.
    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::Blob.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

7 entries across 7 versions & 1 rubygems

Version Path
github-linguist-4.8.0 lib/linguist/blob.rb
github-linguist-4.7.6 lib/linguist/blob.rb
github-linguist-4.7.5 lib/linguist/blob.rb
github-linguist-4.7.4 lib/linguist/blob.rb
github-linguist-4.7.3 lib/linguist/blob.rb
github-linguist-4.7.2 lib/linguist/blob.rb
github-linguist-4.7.1 lib/linguist/blob.rb