Class: Licensee::License
- Inherits:
-
Object
- Object
- Licensee::License
- 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
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
-
.all(options = {}) ⇒ Object
All license objects defined via Licensee (via choosealicense.com).
- .find(key, options = {}) ⇒ Object (also: [], find_by_key)
- .keys ⇒ Object
- .license_dir ⇒ Object
- .license_files ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#content ⇒ Object
(also: #to_s, #text, #body)
The license body (e.g., contents - frontmatter).
-
#content_for_mustache ⇒ Object
Returns a string with ‘[fields]` replaced by `{{fields}}` Does not mangle non-supported fields in the form of `[field]`.
-
#creative_commons? ⇒ Boolean
(also: #cc?)
Is this license a Creative Commons license?.
-
#fields ⇒ Object
Returns an array of strings of substitutable fields in the license body.
- #gpl? ⇒ Boolean
-
#initialize(key) ⇒ License
constructor
A new instance of License.
- #inspect ⇒ Object
- #lgpl? ⇒ Boolean
-
#meta ⇒ Object
License metadata from YAML front matter with defaults merged in.
-
#name ⇒ Object
Returns the human-readable license name.
- #name_without_version ⇒ Object
- #other? ⇒ Boolean
-
#path ⇒ Object
Path to vendored license file on disk.
- #pseudo_license? ⇒ Boolean
- #rules ⇒ Object
- #url ⇒ Object
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
#key ⇒ Object (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( = {}) @all[] ||= begin = { hidden: false, featured: nil }.merge() output = licenses.dup output.reject!(&:hidden?) unless [:hidden] return output if [:featured].nil? output.select { |l| l.featured? == [: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, = {}) = { hidden: true }.merge() keys_licenses()[key.downcase] end |
.keys ⇒ Object
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_dir ⇒ Object
40 41 42 43 |
# File 'lib/licensee/license.rb', line 40 def license_dir dir = ::File.dirname(__FILE__) ::File. '../../vendor/choosealicense.com/_licenses', dir end |
.license_files ⇒ Object
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 |
#content ⇒ Object 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_mustache ⇒ Object
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?
113 114 115 |
# File 'lib/licensee/license.rb', line 113 def creative_commons? key.start_with?('cc-') end |
#fields ⇒ Object
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
104 105 106 |
# File 'lib/licensee/license.rb', line 104 def gpl? key == 'gpl-2.0' || key == 'gpl-3.0' end |
#inspect ⇒ Object
142 143 144 |
# File 'lib/licensee/license.rb', line 142 def inspect "#<Licensee::License key=#{key}>" end |
#lgpl? ⇒ Boolean
108 109 110 |
# File 'lib/licensee/license.rb', line 108 def lgpl? key == 'lgpl-2.1' || key == 'lgpl-3.0' end |
#meta ⇒ Object
License metadata from YAML front matter with defaults merged in
87 88 89 |
# File 'lib/licensee/license.rb', line 87 def ||= LicenseMeta.from_yaml(yaml) end |
#name ⇒ Object
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_version ⇒ Object
96 97 98 |
# File 'lib/licensee/license.rb', line 96 def name_without_version /(.+?)(( v?\d\.\d)|$)/.match(name)[1] end |
#other? ⇒ Boolean
100 101 102 |
# File 'lib/licensee/license.rb', line 100 def other? key == 'other' end |
#path ⇒ Object
Path to vendored license file on disk
82 83 84 |
# File 'lib/licensee/license.rb', line 82 def path @path ||= File. "#{@key}.txt", Licensee::License.license_dir end |
#pseudo_license? ⇒ Boolean
134 135 136 |
# File 'lib/licensee/license.rb', line 134 def pseudo_license? PSEUDO_LICENSES.include?(key) end |
#rules ⇒ Object
138 139 140 |
# File 'lib/licensee/license.rb', line 138 def rules @rules ||= LicenseRules.() end |
#url ⇒ Object
126 127 128 |
# File 'lib/licensee/license.rb', line 126 def url URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s end |