Class: Licensee::Projects::Project

Inherits:
Object
  • Object
show all
Includes:
HashHelper
Defined in:
lib/licensee/projects/project.rb

Direct Known Subclasses

FSProject, GitHubProject, GitProject

Constant Summary collapse

HASH_METHODS =
%i[licenses matched_files].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashHelper

#to_h

Constructor Details

#initialize(detect_packages: false, detect_readme: false) ⇒ Project

Returns a new instance of Project.



18
19
20
21
# File 'lib/licensee/projects/project.rb', line 18

def initialize(detect_packages: false, detect_readme: false, **)
  @detect_packages = detect_packages
  @detect_readme = detect_readme
end

Instance Attribute Details

#detect_packagesObject (readonly) Also known as: detect_packages?

Returns the value of attribute detect_packages.



11
12
13
# File 'lib/licensee/projects/project.rb', line 11

def detect_packages
  @detect_packages
end

#detect_readmeObject (readonly) Also known as: detect_readme?

Returns the value of attribute detect_readme.



11
12
13
# File 'lib/licensee/projects/project.rb', line 11

def detect_readme
  @detect_readme
end

Instance Method Details

#licenseObject

Returns the matching License instance if a license can be detected



24
25
26
27
28
29
30
31
32
# File 'lib/licensee/projects/project.rb', line 24

def license
  return @license if defined? @license

  @license = if licenses_without_copyright.count == 1 || lgpl?
               licenses_without_copyright.first
             elsif licenses_without_copyright.count > 1
               Licensee::License.find('other')
             end
end

#license_fileObject

Returns the LicenseFile used to determine the License



50
51
52
# File 'lib/licensee/projects/project.rb', line 50

def license_file
  license_files.first if license_files.count == 1 || lgpl?
end

#license_filesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/licensee/projects/project.rb', line 54

def license_files
  @license_files ||= if files.empty? || files.nil?
                       []
                     else
                       files = find_files do |n|
                         Licensee::ProjectFiles::LicenseFile.name_score(n)
                       end
                       files = files.map do |file|
                         Licensee::ProjectFiles::LicenseFile.new(load_file(file), file)
                       end
                       prioritize_lgpl(files)
                     end
end

#licensesObject

Returns an array of detected Licenses



35
36
37
# File 'lib/licensee/projects/project.rb', line 35

def licenses
  @licenses ||= matched_files.map(&:license).uniq
end

#matched_fileObject

Returns the ProjectFile used to determine the License



40
41
42
# File 'lib/licensee/projects/project.rb', line 40

def matched_file
  matched_files.first if matched_files.count == 1 || lgpl?
end

#matched_filesObject

Returns an array of matches LicenseFiles



45
46
47
# File 'lib/licensee/projects/project.rb', line 45

def matched_files
  @matched_files ||= project_files.select(&:license)
end

#package_fileObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/licensee/projects/project.rb', line 85

def package_file
  return unless detect_packages?
  return @package_file if defined? @package_file

  @package_file = begin
    content, file = find_file do |n|
      Licensee::ProjectFiles::PackageManagerFile.name_score(n)
    end

    return unless content && file

    Licensee::ProjectFiles::PackageManagerFile.new(content, file)
  end
end

#readme_fileObject Also known as: readme



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/licensee/projects/project.rb', line 68

def readme_file
  return unless detect_readme?
  return @readme if defined? @readme

  @readme = begin
    content, file = find_file do |n|
      Licensee::ProjectFiles::ReadmeFile.name_score(n)
    end
    content = Licensee::ProjectFiles::ReadmeFile.license_content(content)

    return unless content && file

    Licensee::ProjectFiles::ReadmeFile.new(content, file)
  end
end