Sha256: 9d9426fc25c67dfb9a06bed3ef1f705011285f1d4301a827968ff6770fee4f33

Contents?: true

Size: 1.52 KB

Versions: 17

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
      _, *segments = name.downcase.split(".")

      segments.map.with_index do |segment, index|
        "." + segments[index..-1].join(".")
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
github-linguist-4.8.17 lib/linguist/blob.rb
github-linguist-4.8.16 lib/linguist/blob.rb
github-linguist-4.8.15 lib/linguist/blob.rb
github-linguist-4.8.14 lib/linguist/blob.rb
github-linguist-4.8.13 lib/linguist/blob.rb
github-linguist-4.8.12 lib/linguist/blob.rb
github-linguist-4.8.11 lib/linguist/blob.rb
github-linguist-4.8.10 lib/linguist/blob.rb
github-linguist-4.8.9 lib/linguist/blob.rb
github-linguist-4.8.8 lib/linguist/blob.rb
github-linguist-4.8.7 lib/linguist/blob.rb
github-linguist-4.8.6 lib/linguist/blob.rb
github-linguist-4.8.5 lib/linguist/blob.rb
github-linguist-4.8.4 lib/linguist/blob.rb
github-linguist-4.8.3 lib/linguist/blob.rb
github-linguist-4.8.2 lib/linguist/blob.rb
github-linguist-4.8.1 lib/linguist/blob.rb