Class: Licensee::License
- Inherits:
-
Object
- Object
- Licensee::License
- Defined in:
- lib/licensee/license.rb
Constant Summary collapse
- YAML_DEFAULTS =
{ "featured" => false, "hidden" => false, "variant" => false }
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
-
#body ⇒ Object
(also: #to_s, #text)
The license body (e.g., contents - frontmatter).
-
#body_normalized ⇒ Object
License body with all whitespace replaced with a single space.
-
#content ⇒ Object
Raw content of license file, including YAML front matter.
- #featured? ⇒ Boolean (also: #featured)
-
#hashsig ⇒ Object
Git-computed hash signature for the license file.
-
#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.
-
#path ⇒ Object
Path to vendored license file on disk.
- #url ⇒ Object
Constructor Details
#initialize(key) ⇒ License
Returns a new instance of License.
17 18 19 |
# File 'lib/licensee/license.rb', line 17 def initialize(key) @key=key.downcase end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/licensee/license.rb', line 9 def key @key end |
Class Method Details
.all ⇒ Object
5 6 7 |
# File 'lib/licensee/license.rb', line 5 def self.all Licensee::licenses end |
Instance Method Details
#body ⇒ Object Also known as: to_s, text
The license body (e.g., contents - frontmatter)
60 61 62 |
# File 'lib/licensee/license.rb', line 60 def body @body ||= parts[2] if parts && parts[2] end |
#body_normalized ⇒ Object
License body with all whitespace replaced with a single space
67 68 69 |
# File 'lib/licensee/license.rb', line 67 def body_normalized @content_normalized ||= body.to_s.downcase.gsub(/\s+/, " ").strip end |
#content ⇒ Object
Raw content of license file, including YAML front matter
27 28 29 30 31 32 33 34 35 |
# File 'lib/licensee/license.rb', line 27 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
54 55 56 |
# File 'lib/licensee/license.rb', line 54 def featured? !!(["featured"] if ) end |
#hashsig ⇒ Object
Git-computed hash signature for the license file
72 73 74 75 76 77 |
# File 'lib/licensee/license.rb', line 72 def hashsig @hashsig ||= Rugged::Blob::HashSignature.new( body, Rugged::Blob::HashSignature::WHITESPACE_SMART) rescue Rugged::InvalidError nil end |
#inspect ⇒ Object
79 80 81 |
# File 'lib/licensee/license.rb', line 79 def inspect "#<Licensee::License key=\"#{key}\" name=\"#{name}\">" end |
#meta ⇒ Object
License metadata from YAML front matter
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/licensee/license.rb', line 38 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
50 51 52 |
# File 'lib/licensee/license.rb', line 50 def name .nil? ? key.capitalize : ["title"] end |
#path ⇒ Object
Path to vendored license file on disk
22 23 24 |
# File 'lib/licensee/license.rb', line 22 def path @path ||= File. "#{@key}.txt", Licensee::Licenses.base end |
#url ⇒ Object
83 84 85 |
# File 'lib/licensee/license.rb', line 83 def url URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s end |