Module: Licensee::License::ClassMethods

Included in:
Licensee::License
Defined in:
lib/licensee/license/class_methods.rb

Overview

Class-level lookup and caching for licenses.

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object

All license objects defined via Licensee (via choosealicense.com)

Options:

  • :hidden - boolean, return hidden licenses (default: false)

  • :featured - boolean, return only (non)featured licenses (default: all)

Returns an Array of License objects.



14
15
16
17
18
19
20
21
22
# File 'lib/licensee/license/class_methods.rb', line 14

def all(options = {})
  @all[options] ||= begin
    normalized_options = LicenseAllHelper.normalize_all_options(options, DEFAULT_OPTIONS)
    output = licenses.dup
    LicenseAllHelper.apply_all_filters!(output, normalized_options)
    output.sort_by!(&:key)
    LicenseAllHelper.filter_featured(output, normalized_options[:featured])
  end
end

#find(key, options = {}) ⇒ Object Also known as: [], find_by_key



30
31
32
33
# File 'lib/licensee/license/class_methods.rb', line 30

def find(key, options = {})
  options = { hidden: true }.merge(options)
  keys_licenses(options)[key.downcase]
end

#find_by_title(title) ⇒ Object

Given a license title or nickname, fuzzy match the license



38
39
40
41
42
# File 'lib/licensee/license/class_methods.rb', line 38

def find_by_title(title)
  License.all(hidden: true, pseudo: false).find do |license|
    title =~ /\A(the )?#{license.title_regex}( license)?\z/i
  end
end

#keysObject



24
25
26
27
28
# File 'lib/licensee/license/class_methods.rb', line 24

def keys
  @keys ||= license_files.map do |license_file|
    ::File.basename(license_file, '.txt').downcase
  end + PSEUDO_LICENSES
end

#license_dirObject



44
45
46
# File 'lib/licensee/license/class_methods.rb', line 44

def license_dir
  ::File.expand_path '../../../vendor/choosealicense.com/_licenses', __dir__
end

#license_filesObject



48
49
50
# File 'lib/licensee/license/class_methods.rb', line 48

def license_files
  @license_files ||= Dir.glob("#{license_dir}/*.txt")
end

#spdx_dirObject



52
53
54
# File 'lib/licensee/license/class_methods.rb', line 52

def spdx_dir
  ::File.expand_path '../../../vendor/license-list-XML/src', __dir__
end