File: blob.rb

package info (click to toggle)
ruby-github-linguist 7.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 14,204 kB
  • sloc: ruby: 1,872; lex: 173; ansic: 35; makefile: 9
file content (82 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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.
    # symlink - Whether the file is a symlink.
    #
    # Returns a Blob.
    def initialize(path, content, symlink: false)
      @path = path
      @content = content
      @symlink = symlink
    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(".", -1)

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

    # Public: Is this a symlink?
    #
    # Returns true or false.
    def symlink?
      @symlink
    end
  end
end