Class: Linguist::LazyBlob

Inherits:
Object
  • Object
show all
Includes:
BlobHelper
Defined in:
lib/linguist/lazy_blob.rb

Constant Summary collapse

GIT_ATTR =
['linguist-documentation',
'linguist-language',
'linguist-vendored',
'linguist-generated',
'linguist-detectable']
GIT_ATTR_OPTS =

DEPRECATED: use Linguist::Source::RuggedRepository::GIT_ATTR_OPTS instead

Linguist::Source::RuggedRepository::GIT_ATTR_OPTS
GIT_ATTR_FLAGS =

DEPRECATED: use Linguist::Source::RuggedRepository::GIT_ATTR_FLAGS instead

Linguist::Source::RuggedRepository::GIT_ATTR_FLAGS
MAX_SIZE =
128 * 1024

Constants included from BlobHelper

BlobHelper::DETECTABLE_TYPES, BlobHelper::DocumentationRegexp, BlobHelper::MEGABYTE, BlobHelper::VendoredRegexp

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BlobHelper

#_mime_type, #binary?, #binary_mime_type?, #content_type, #csv?, #detect_encoding, #disposition, #empty?, #encoded_newlines_re, #encoding, #extname, #first_lines, #high_ratio_of_long_lines?, #image?, #include_in_language_stats?, #large?, #last_lines, #likely_binary?, #lines, #loc, #mime_type, #pdf?, #ruby_encoding, #safe_to_colorize?, #sloc, #solid?, #text?, #tm_scope, #viewable?

Constructor Details

#initialize(repo, oid, path, mode = nil) ⇒ LazyBlob

Returns a new instance of LazyBlob.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/linguist/lazy_blob.rb', line 31

def initialize(repo, oid, path, mode = nil)
  @repository = if repo.is_a? Linguist::Source::Repository
    repo
  else
    # Allow this for backward-compatibility purposes
    Linguist::Source::RuggedRepository.new(repo)
  end
  @oid = oid
  @path = path
  @mode = mode
  @data = nil
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



27
28
29
# File 'lib/linguist/lazy_blob.rb', line 27

def mode
  @mode
end

#oidObject (readonly)

Returns the value of attribute oid.



25
26
27
# File 'lib/linguist/lazy_blob.rb', line 25

def oid
  @oid
end

#pathObject (readonly) Also known as: name

Returns the value of attribute path.



26
27
28
# File 'lib/linguist/lazy_blob.rb', line 26

def path
  @path
end

#repositoryObject (readonly)

Returns the value of attribute repository.



24
25
26
# File 'lib/linguist/lazy_blob.rb', line 24

def repository
  @repository
end

Instance Method Details

#cleanup!Object



123
124
125
# File 'lib/linguist/lazy_blob.rb', line 123

def cleanup!
  @data.clear if @data
end

#dataObject



108
109
110
111
# File 'lib/linguist/lazy_blob.rb', line 108

def data
  load_blob!
  @data
end

#detectable?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
# File 'lib/linguist/lazy_blob.rb', line 100

def detectable?
  if not git_attributes['linguist-detectable'].nil?
    boolean_attribute(git_attributes['linguist-detectable'])
  else
    nil
  end
end

#documentation?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/linguist/lazy_blob.rb', line 48

def documentation?
  if not git_attributes['linguist-documentation'].nil?
    boolean_attribute(git_attributes['linguist-documentation'])
  else
    super
  end
end

#generated?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/linguist/lazy_blob.rb', line 56

def generated?
  if not git_attributes['linguist-generated'].nil?
    boolean_attribute(git_attributes['linguist-generated'])
  else
    super
  end
end

#git_attributesObject



44
45
46
# File 'lib/linguist/lazy_blob.rb', line 44

def git_attributes
  @git_attributes ||= repository.load_attributes_for_path(name, GIT_ATTR)
end

#languageObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/linguist/lazy_blob.rb', line 72

def language
  return @language if defined?(@language)

  @language = if lang = git_attributes['linguist-language']
    detected_language = Language.find_by_alias(lang)

    # If strategies are being tracked, get the original strategy that would have been used
    if detected_language && Linguist.instrumenter
      # Get the original strategy by calling super (which calls Linguist.detect)
      original_language = super
      original_strategy_info = Linguist.instrumenter.detected_info[self.name]
      original_strategy = original_strategy_info ? original_strategy_info[:strategy] : "Unknown"

      if original_language == detected_language
        strategy_name = "#{original_strategy} (confirmed by .gitattributes)"
      else
        strategy_name = "#{original_strategy} (overridden by .gitattributes)"
      end

      strategy = Struct.new(:name).new(strategy_name)
      Linguist.instrument("linguist.detected", blob: self, strategy: strategy, language: detected_language)
    end
    detected_language
  else
    super
  end
end

#sizeObject



113
114
115
116
# File 'lib/linguist/lazy_blob.rb', line 113

def size
  load_blob!
  @size
end

#symlink?Boolean

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/linguist/lazy_blob.rb', line 118

def symlink?
  # We don't create LazyBlobs for symlinks.
  false
end

#vendored?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/linguist/lazy_blob.rb', line 64

def vendored?
  if not git_attributes['linguist-vendored'].nil?
    boolean_attribute(git_attributes['linguist-vendored'])
  else
    super
  end
end