Class: Licensee::License

Inherits:
Object
  • Object
show all
Defined in:
lib/licensee/license.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ License

Returns a new instance of License.



11
12
13
# File 'lib/licensee/license.rb', line 11

def initialize(key)
  @key=key.downcase
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/licensee/license.rb', line 9

def key
  @key
end

Class Method Details

.allObject



5
6
7
# File 'lib/licensee/license.rb', line 5

def self.all
  Licensee::licenses
end

Instance Method Details

#bodyObject Also known as: to_s, text

The license body (e.g., contents - frontmatter)



47
48
49
# File 'lib/licensee/license.rb', line 47

def body
  @body ||= parts[2] if parts && parts[2]
end

#body_normalizedObject

License body with all whitespace replaced with a single space



54
55
56
# File 'lib/licensee/license.rb', line 54

def body_normalized
  @content_normalized ||= body.to_s.downcase.gsub(/\s+/, " ").strip
end

#contentObject

Raw content of license file, including YAML front matter



21
22
23
24
25
26
27
28
29
# File 'lib/licensee/license.rb', line 21

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/licensee/license.rb', line 41

def featured?
  !!(meta["featured"] if meta)
end

#hashsigObject

Git-computed hash signature for the license file



59
60
61
62
63
64
# File 'lib/licensee/license.rb', line 59

def hashsig
  @hashsig ||= Rugged::Blob::HashSignature.new(
    body, Rugged::Blob::HashSignature::WHITESPACE_SMART)
rescue Rugged::InvalidError
  nil
end

#inspectObject



66
67
68
# File 'lib/licensee/license.rb', line 66

def inspect
  "#<Licensee::License key=\"#{key}\" name=\"#{name}\">"
end

#metaObject

License metadata from YAML front matter



32
33
34
# File 'lib/licensee/license.rb', line 32

def meta
  @meta ||= YAML.load(parts[1]) if parts && parts[1]
end

#nameObject

Returns the human-readable license name



37
38
39
# File 'lib/licensee/license.rb', line 37

def name
  meta.nil? ? key.capitalize : meta["title"]
end

#pathObject

Path to vendored license file on disk



16
17
18
# File 'lib/licensee/license.rb', line 16

def path
  @path ||= File.expand_path "#{@key}.txt", Licensee::Licenses.base
end

#urlObject



70
71
72
# File 'lib/licensee/license.rb', line 70

def url
  URI.join(Licensee::DOMAIN, meta["permalink"]).to_s
end