Class: Licensee::ProjectFiles::ProjectFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
HashHelper
Defined in:
lib/licensee/project_files/project_file.rb

Overview

Base class for a file within a project that may contain license information.

Direct Known Subclasses

LicenseFile, PackageManagerFile

Constant Summary collapse

HASH_METHODS =
%i[
  filename content content_hash content_normalized matcher matched_license
  attribution
].freeze
ENCODING =
Encoding::UTF_8
ENCODING_OPTIONS =
{
  invalid: :replace,
  undef:   :replace,
  replace: ''
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashHelper

#serialize_hash_value, #to_h

Constructor Details

#initialize(content, metadata = {}) ⇒ ProjectFile

Create a new Licensee::ProjectFile with content and metadata

content - file content metadata - can be either the string filename, or a hash containing

metadata about the file content. If a hash is given, the
filename should be given using the :name key. See individual
project types for additional available metadata

Returns a new Licensee::ProjectFile



40
41
42
43
44
45
46
47
48
# File 'lib/licensee/project_files/project_file.rb', line 40

def initialize(content,  = {})
  @content = content.dup
  @content.force_encoding(ENCODING)
  @content.encode!(ENCODING, **ENCODING_OPTIONS) unless @content.valid_encoding?
  @content.encode!(ENCODING, universal_newline: true)

   = { name:  } if .is_a? String
  @data =  || {}
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



15
16
17
# File 'lib/licensee/project_files/project_file.rb', line 15

def content
  @content
end

Instance Method Details

#attributionObject



108
109
110
# File 'lib/licensee/project_files/project_file.rb', line 108

def attribution
  nil
end

#confidenceObject

Returns the percent confident with the match



77
78
79
# File 'lib/licensee/project_files/project_file.rb', line 77

def confidence
  matcher&.confidence
end

#content_hashObject



100
101
102
# File 'lib/licensee/project_files/project_file.rb', line 100

def content_hash
  nil
end

#content_normalizedObject



104
105
106
# File 'lib/licensee/project_files/project_file.rb', line 104

def content_normalized
  nil
end

#copyright?Boolean

Is this file a COPYRIGHT file with only a copyright statement? If so, it can be excluded from determining if a project has >1 license

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/licensee/project_files/project_file.rb', line 93

def copyright?
  return false unless is_a?(LicenseFile)
  return false unless matcher.is_a?(Matchers::Copyright)

  filename =~ /\Acopyright(?:#{LicenseFile::OTHER_EXT_REGEX})?\z/io
end

#directoryObject Also known as: dir



58
59
60
# File 'lib/licensee/project_files/project_file.rb', line 58

def directory
  @data[:dir] || '.'
end

#filenameObject



50
51
52
# File 'lib/licensee/project_files/project_file.rb', line 50

def filename
  @data[:name]
end

#licenseObject Also known as: match



81
82
83
# File 'lib/licensee/project_files/project_file.rb', line 81

def license
  matcher&.match
end

#matched_licenseObject



87
88
89
# File 'lib/licensee/project_files/project_file.rb', line 87

def matched_license
  license&.spdx_id
end

#matcherObject



72
73
74
# File 'lib/licensee/project_files/project_file.rb', line 72

def matcher
  @matcher ||= possible_matchers.map { |m| m.new(self) }.find(&:match)
end

#pathObject



54
55
56
# File 'lib/licensee/project_files/project_file.rb', line 54

def path
  Pathname.new(directory).join(@data[:name]).cleanpath.to_s
end

#path_relative_to_rootObject Also known as: relative_path



63
64
65
# File 'lib/licensee/project_files/project_file.rb', line 63

def path_relative_to_root
  File.join(directory, filename)
end

#possible_matchersObject

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/licensee/project_files/project_file.rb', line 68

def possible_matchers
  raise NotImplementedError, "#{self.class}#possible_matchers is not implemented"
end