Class: Licensee::License
- Inherits:
-
Object
- Object
- Licensee::License
- Includes:
- ContentHelper
- Defined in:
- lib/licensee/license.rb
Constant Summary collapse
- YAML_DEFAULTS =
{ "featured" => false, "hidden" => false, "variant" => false }
- HIDDEN_LICENSES =
%w[other no-license]
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
- .all(options = {}) ⇒ Object
- .find(key, options = {}) ⇒ Object (also: [], find_by_key)
- .keys ⇒ Object
- .license_dir ⇒ Object
- .license_files ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#body ⇒ Object
(also: #to_s, #text)
The license body (e.g., contents - frontmatter).
-
#content ⇒ Object
Raw content of license file, including YAML front matter.
- #featured? ⇒ Boolean (also: #featured)
- #hidden? ⇒ Boolean
-
#initialize(key) ⇒ License
constructor
A new instance of License.
- #inspect ⇒ Object
-
#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
- #wordset ⇒ Object
Methods included from ContentHelper
Constructor Details
#initialize(key) ⇒ License
Returns a new instance of License.
47 48 49 |
# File 'lib/licensee/license.rb', line 47 def initialize(key) @key = key.downcase end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
35 36 37 |
# File 'lib/licensee/license.rb', line 35 def key @key end |
Class Method Details
.all(options = {}) ⇒ Object
9 10 11 12 |
# File 'lib/licensee/license.rb', line 9 def all(={}) @all ||= keys.map { |key| self.new(key) } [:hidden] ? @all : @all.reject { |l| l.hidden? } end |
.find(key, options = {}) ⇒ Object Also known as: [], find_by_key
18 19 20 21 22 |
# File 'lib/licensee/license.rb', line 18 def find(key, ={}) = {:hidden => true}.merge() key = key.downcase all().find { |license| license.key == key } end |
.keys ⇒ Object
14 15 16 |
# File 'lib/licensee/license.rb', line 14 def keys @keys ||= license_files.map { |l| File.basename(l, ".txt").downcase } + ["other"] end |
.license_dir ⇒ Object
26 27 28 |
# File 'lib/licensee/license.rb', line 26 def license_dir File. "../../vendor/choosealicense.com/_licenses", File.dirname(__FILE__) end |
.license_files ⇒ Object
30 31 32 |
# File 'lib/licensee/license.rb', line 30 def license_files @license_files ||= Dir.glob("#{license_dir}/*.txt") end |
Instance Method Details
#==(other) ⇒ Object
121 122 123 |
# File 'lib/licensee/license.rb', line 121 def ==(other) other != nil && key == other.key end |
#body ⇒ Object Also known as: to_s, text
The license body (e.g., contents - frontmatter)
103 104 105 |
# File 'lib/licensee/license.rb', line 103 def body @body ||= parts[2] if parts && parts[2] end |
#content ⇒ Object
Raw content of license file, including YAML front matter
57 58 59 60 61 62 63 64 65 |
# File 'lib/licensee/license.rb', line 57 def content @content ||= if File.exists?(path) File.open(path).read elsif key == "other" # A pseudo-license with no content nil else raise Licensee::InvalidLicense, "'#{key}' is not a valid license key" end end |
#featured? ⇒ Boolean Also known as: featured
92 93 94 |
# File 'lib/licensee/license.rb', line 92 def featured? !!(["featured"] if ) end |
#hidden? ⇒ Boolean
97 98 99 100 |
# File 'lib/licensee/license.rb', line 97 def hidden? return true if HIDDEN_LICENSES.include?(key) !!(["hidden"] if ) end |
#inspect ⇒ Object
113 114 115 |
# File 'lib/licensee/license.rb', line 113 def inspect "#<Licensee::License key=\"#{key}\" name=\"#{name}\">" end |
#meta ⇒ Object
License metadata from YAML front matter
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/licensee/license.rb', line 68 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
80 81 82 |
# File 'lib/licensee/license.rb', line 80 def name .nil? ? key.capitalize : ["title"] end |
#name_without_version ⇒ Object
88 89 90 |
# File 'lib/licensee/license.rb', line 88 def name_without_version /(.+?)(( v?\d\.\d)|$)/.match(name)[1] end |
#nickname ⇒ Object
84 85 86 |
# File 'lib/licensee/license.rb', line 84 def nickname ["nickname"] if end |
#path ⇒ Object
Path to vendored license file on disk
52 53 54 |
# File 'lib/licensee/license.rb', line 52 def path @path ||= File. "#{@key}.txt", Licensee::License.license_dir end |
#url ⇒ Object
117 118 119 |
# File 'lib/licensee/license.rb', line 117 def url URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s end |
#wordset ⇒ Object
109 110 111 |
# File 'lib/licensee/license.rb', line 109 def wordset @wordset ||= create_word_set(body) end |