Class: Licensee::License

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ContentHelper
Defined in:
lib/licensee/license.rb

Constant Summary collapse

YAML_DEFAULTS =

Preserved for backwards compatibility

Licensee::LicenseMeta.members
PSEUDO_LICENSES =

Pseudo-license are license placeholders with no content

‘other` - The project had a license, but we were not able to detect it `no-license` - The project is not licensed (e.g., all rights reserved)

Note: A lack of detected license will be a nil license

%w[other no-license].freeze

Constants included from ContentHelper

ContentHelper::ALL_RIGHTS_RESERVED_REGEX, ContentHelper::ALT_TITLE_REGEX, ContentHelper::DIGEST, ContentHelper::END_OF_TERMS_REGEX, ContentHelper::HR_REGEX, ContentHelper::MARKDOWN_HEADING_REGEX, ContentHelper::VERSION_REGEX, ContentHelper::WHITESPACE_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContentHelper

#content_hash, #content_normalized, #content_without_title_and_version, format_percent, #length, #length_delta, #max_delta, #similarity, #wordset, wrap

Constructor Details

#initialize(key) ⇒ License

Returns a new instance of License.



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

def initialize(key)
  @key = key.downcase
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



60
61
62
# File 'lib/licensee/license.rb', line 60

def key
  @key
end

Class 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.



17
18
19
20
21
22
23
24
25
# File 'lib/licensee/license.rb', line 17

def all(options = {})
  @all[options] ||= begin
    options = { hidden: false, featured: nil }.merge(options)
    output = licenses.dup
    output.reject!(&:hidden?) unless options[:hidden]
    return output if options[:featured].nil?
    output.select { |l| l.featured? == options[:featured] }
  end
end

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



33
34
35
36
# File 'lib/licensee/license.rb', line 33

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

.keysObject



27
28
29
30
31
# File 'lib/licensee/license.rb', line 27

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

.license_dirObject



40
41
42
43
# File 'lib/licensee/license.rb', line 40

def license_dir
  dir = ::File.dirname(__FILE__)
  ::File.expand_path '../../vendor/choosealicense.com/_licenses', dir
end

.license_filesObject



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

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

Instance Method Details

#==(other) ⇒ Object



130
131
132
# File 'lib/licensee/license.rb', line 130

def ==(other)
  !other.nil? && key == other.key
end

#contentObject Also known as: to_s, text, body

The license body (e.g., contents - frontmatter)



119
120
121
# File 'lib/licensee/license.rb', line 119

def content
  @content ||= parts[2] if parts && parts[2]
end

#content_for_mustacheObject

Returns a string with ‘[fields]` replaced by `{{fields}}` Does not mangle non-supported fields in the form of `[field]`



153
154
155
156
157
# File 'lib/licensee/license.rb', line 153

def content_for_mustache
  @content_for_mustache ||= begin
    content.gsub(LicenseField::FIELD_REGEX, '{{{\1}}}')
  end
end

#creative_commons?Boolean Also known as: cc?

Is this license a Creative Commons license?

Returns:

  • (Boolean)


113
114
115
# File 'lib/licensee/license.rb', line 113

def creative_commons?
  key.start_with?('cc-')
end

#fieldsObject

Returns an array of strings of substitutable fields in the license body



147
148
149
# File 'lib/licensee/license.rb', line 147

def fields
  @fields ||= LicenseField.from_content(content)
end

#gpl?Boolean

Returns:

  • (Boolean)


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

def gpl?
  key == 'gpl-2.0' || key == 'gpl-3.0'
end

#inspectObject



142
143
144
# File 'lib/licensee/license.rb', line 142

def inspect
  "#<Licensee::License key=#{key}>"
end

#lgpl?Boolean

Returns:

  • (Boolean)


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

def lgpl?
  key == 'lgpl-2.1' || key == 'lgpl-3.0'
end

#metaObject

License metadata from YAML front matter with defaults merged in



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

def meta
  @meta ||= LicenseMeta.from_yaml(yaml)
end

#nameObject

Returns the human-readable license name



92
93
94
# File 'lib/licensee/license.rb', line 92

def name
  title ? title : key.capitalize
end

#name_without_versionObject



96
97
98
# File 'lib/licensee/license.rb', line 96

def name_without_version
  /(.+?)(( v?\d\.\d)|$)/.match(name)[1]
end

#other?Boolean

Returns:

  • (Boolean)


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

def other?
  key == 'other'
end

#pathObject

Path to vendored license file on disk



82
83
84
# File 'lib/licensee/license.rb', line 82

def path
  @path ||= File.expand_path "#{@key}.txt", Licensee::License.license_dir
end

#pseudo_license?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/licensee/license.rb', line 134

def pseudo_license?
  PSEUDO_LICENSES.include?(key)
end

#rulesObject



138
139
140
# File 'lib/licensee/license.rb', line 138

def rules
  @rules ||= LicenseRules.from_meta(meta)
end

#urlObject



126
127
128
# File 'lib/licensee/license.rb', line 126

def url
  URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s
end