Class: Licensee::License
- Inherits:
-
Object
- Object
- Licensee::License
- Includes:
- ContentHelper
- Defined in:
- lib/licensee/license.rb
Constant Summary collapse
- YAML_DEFAULTS =
These should be in sync with choosealicense.com’s collection defaults
{ 'featured' => false, 'hidden' => true }.freeze
- 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::DIGEST, ContentHelper::END_OF_TERMS_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).
-
#creative_commons? ⇒ Boolean
Is this license a Creative Commons license?.
- #featured? ⇒ Boolean (also: #featured)
- #gpl? ⇒ Boolean
- #hidden? ⇒ Boolean
-
#initialize(key) ⇒ License
constructor
A new instance of License.
- #inspect ⇒ Object
-
#meta ⇒ Object
License metadata from YAML front matter with defaults merged in.
-
#name ⇒ Object
Returns the human-readable license name.
- #name_without_version ⇒ Object
- #nickname ⇒ Object
-
#path ⇒ Object
Path to vendored license file on disk.
- #pseudo_license? ⇒ Boolean
-
#rules ⇒ Object
Returns a hash in the form of rule_group => rules describing what you legally can and can’t do with the given license.
- #url ⇒ Object
Methods included from ContentHelper
#content_normalized, #hash, #length, #length_delta, #max_delta, #similarity, #wordset
Constructor Details
#initialize(key) ⇒ License
Returns a new instance of License.
70 71 72 |
# File 'lib/licensee/license.rb', line 70 def initialize(key) @key = key.downcase end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
52 53 54 |
# File 'lib/licensee/license.rb', line 52 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.
15 16 17 18 19 20 21 |
# File 'lib/licensee/license.rb', line 15 def all( = {}) = { 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 |
.find(key, options = {}) ⇒ Object Also known as: [], find_by_key
29 30 31 32 |
# File 'lib/licensee/license.rb', line 29 def find(key, = {}) = { hidden: true }.merge() all().find { |license| key.casecmp(license.key).zero? } end |
.keys ⇒ Object
23 24 25 26 27 |
# File 'lib/licensee/license.rb', line 23 def keys @keys ||= license_files.map do |license_file| File.basename(license_file, '.txt').downcase end + PSEUDO_LICENSES end |
.license_dir ⇒ Object
36 37 38 39 |
# File 'lib/licensee/license.rb', line 36 def license_dir dir = File.dirname(__FILE__) File. '../../vendor/choosealicense.com/_licenses', dir end |
.license_files ⇒ Object
41 42 43 |
# File 'lib/licensee/license.rb', line 41 def license_files @license_files ||= Dir.glob("#{license_dir}/*.txt") end |
Instance Method Details
#==(other) ⇒ Object
131 132 133 |
# File 'lib/licensee/license.rb', line 131 def ==(other) !other.nil? && key == other.key end |
#content ⇒ Object Also known as: to_s, text, body
The license body (e.g., contents - frontmatter)
120 121 122 |
# File 'lib/licensee/license.rb', line 120 def content @content ||= parts[2] if parts && parts[2] end |
#creative_commons? ⇒ Boolean
Is this license a Creative Commons license?
115 116 117 |
# File 'lib/licensee/license.rb', line 115 def creative_commons? key.start_with?('cc-') end |
#featured? ⇒ Boolean Also known as: featured
101 102 103 |
# File 'lib/licensee/license.rb', line 101 def featured? ['featured'] end |
#gpl? ⇒ Boolean
110 111 112 |
# File 'lib/licensee/license.rb', line 110 def gpl? key == 'gpl-2.0' || key == 'gpl-3.0' end |
#hidden? ⇒ Boolean
106 107 108 |
# File 'lib/licensee/license.rb', line 106 def hidden? ['hidden'] end |
#inspect ⇒ Object
152 153 154 |
# File 'lib/licensee/license.rb', line 152 def inspect "#<Licensee::License key=#{key}>" end |
#meta ⇒ Object
License metadata from YAML front matter with defaults merged in
80 81 82 83 84 85 86 |
# File 'lib/licensee/license.rb', line 80 def @meta ||= begin return YAML_DEFAULTS unless parts && parts[1] = YAML.safe_load(parts[1]) YAML_DEFAULTS.merge() end end |
#name ⇒ Object
Returns the human-readable license name
89 90 91 |
# File 'lib/licensee/license.rb', line 89 def name ['title'] ? ['title'] : key.capitalize end |
#name_without_version ⇒ Object
97 98 99 |
# File 'lib/licensee/license.rb', line 97 def name_without_version /(.+?)(( v?\d\.\d)|$)/.match(name)[1] end |
#nickname ⇒ Object
93 94 95 |
# File 'lib/licensee/license.rb', line 93 def nickname ['nickname'] end |
#path ⇒ Object
Path to vendored license file on disk
75 76 77 |
# File 'lib/licensee/license.rb', line 75 def path @path ||= File. "#{@key}.txt", Licensee::License.license_dir end |
#pseudo_license? ⇒ Boolean
135 136 137 |
# File 'lib/licensee/license.rb', line 135 def pseudo_license? PSEUDO_LICENSES.include?(key) end |
#rules ⇒ Object
Returns a hash in the form of rule_group => rules describing what you legally can and can’t do with the given license
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/licensee/license.rb', line 141 def rules return @rules if defined? @rules @rules = {} Rule.groups.each do |group| @rules[group] = [group].map { |tag| Rule.find_by_tag(tag) } end @rules end |