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
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).
- #featured? ⇒ Boolean (also: #featured)
- #hidden? ⇒ Boolean
-
#initialize(key) ⇒ License
constructor
A new instance of License.
-
#meta ⇒ Object
License metadata from YAML front matter.
-
#name ⇒ Object
Returns the human-readable license name.
- #name_without_version ⇒ Object
- #nickname ⇒ Object
-
#path ⇒ Object
Path to vendored license file on disk.
- #url ⇒ Object
Methods included from ContentHelper
#content_normalized, #hash, #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
128 129 130 |
# File 'lib/licensee/license.rb', line 128 def ==(other) !other.nil? && key == other.key end |
#content ⇒ Object Also known as: to_s, text, body
The license body (e.g., contents - frontmatter)
117 118 119 |
# File 'lib/licensee/license.rb', line 117 def content @content ||= parts[2] if parts && parts[2] end |
#featured? ⇒ Boolean Also known as: featured
104 105 106 107 |
# File 'lib/licensee/license.rb', line 104 def featured? return YAML_DEFAULTS['featured'] unless ['featured'] end |
#hidden? ⇒ Boolean
110 111 112 113 114 |
# File 'lib/licensee/license.rb', line 110 def hidden? return true if PSEUDO_LICENSES.include?(key) return YAML_DEFAULTS['hidden'] unless ['hidden'] end |
#meta ⇒ Object
License metadata from YAML front matter
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/licensee/license.rb', line 80 def ||= if parts && parts[1] = if YAML.respond_to? :safe_load YAML.safe_load(parts[1]) else YAML.load(parts[1]) end YAML_DEFAULTS.merge() end end |
#name ⇒ Object
Returns the human-readable license name
92 93 94 |
# File 'lib/licensee/license.rb', line 92 def name .nil? ? key.capitalize : ['title'] end |
#name_without_version ⇒ Object
100 101 102 |
# File 'lib/licensee/license.rb', line 100 def name_without_version /(.+?)(( v?\d\.\d)|$)/.match(name)[1] end |
#nickname ⇒ Object
96 97 98 |
# File 'lib/licensee/license.rb', line 96 def nickname ['nickname'] if 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 |
#url ⇒ Object
124 125 126 |
# File 'lib/licensee/license.rb', line 124 def url URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s end |