Class: Licensee::ProjectFiles::ProjectFile

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

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

#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



37
38
39
40
41
42
43
44
45
46
# File 'lib/licensee/project_files/project_file.rb', line 37

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

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

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/licensee/project_files/project_file.rb', line 13

def content
  @content
end

Instance Method Details

#attributionObject



106
107
108
# File 'lib/licensee/project_files/project_file.rb', line 106

def attribution
  nil
end

#confidenceObject

Returns the percent confident with the match



75
76
77
# File 'lib/licensee/project_files/project_file.rb', line 75

def confidence
  matcher&.confidence
end

#content_hashObject



98
99
100
# File 'lib/licensee/project_files/project_file.rb', line 98

def content_hash
  nil
end

#content_normalizedObject



102
103
104
# File 'lib/licensee/project_files/project_file.rb', line 102

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)


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

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

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

#directoryObject Also known as: dir



56
57
58
# File 'lib/licensee/project_files/project_file.rb', line 56

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

#filenameObject Also known as: path

TODO: In the next major release, filename should be the basename and path should be either the absolute path or the relative path to the project root, but maintaining the alias for backward compatability



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

def filename
  @data[:name]
end

#licenseObject Also known as: match



79
80
81
# File 'lib/licensee/project_files/project_file.rb', line 79

def license
  matcher&.match
end

#matched_licenseObject



85
86
87
# File 'lib/licensee/project_files/project_file.rb', line 85

def matched_license
  license&.spdx_id
end

#matcherObject



70
71
72
# File 'lib/licensee/project_files/project_file.rb', line 70

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

#path_relative_to_rootObject Also known as: relative_path



61
62
63
# File 'lib/licensee/project_files/project_file.rb', line 61

def path_relative_to_root
  File.join(directory, filename)
end

#possible_matchersObject



66
67
68
# File 'lib/licensee/project_files/project_file.rb', line 66

def possible_matchers
  raise 'Not implemented'
end