Class: Licensee::License

Inherits:
Object
  • Object
show all
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]

Constants included from ContentHelper

ContentHelper::DIGEST

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContentHelper

#content_normalized, #hash, #wordset

Constructor Details

#initialize(key) ⇒ License

Returns a new instance of License.



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

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



50
51
52
# File 'lib/licensee/license.rb', line 50

def key
  @key
end

Class Method Details

.all(options = {}) ⇒ Object

All license objects defined via Licensee (via choosealicense.com)

Options - :hidden - boolean, whether to return hidden licenses, defaults to false Options - :featured - boolean, whether to return only (non)featured licenses, defaults to all

Returns an Array of License objects.



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

def all(options={})
  output = licenses.dup
  output.reject! { |l| l.hidden? } unless options[:hidden]
  output.select! { |l| l.featured? == options[:featured] } unless options[:featured].nil?
  output
end

.find(key, options = {}) ⇒ Object Also known as: [], find_by_key



27
28
29
30
31
# File 'lib/licensee/license.rb', line 27

def find(key, options={})
  options = {:hidden => true}.merge(options)
  key = key.downcase
  all(options).find { |license| license.key == key }
end

.keysObject



23
24
25
# File 'lib/licensee/license.rb', line 23

def keys
  @keys ||= license_files.map { |l| File.basename(l, ".txt").downcase } + ["other"]
end

.license_dirObject



35
36
37
# File 'lib/licensee/license.rb', line 35

def license_dir
  File.expand_path "../../vendor/choosealicense.com/_licenses", File.dirname(__FILE__)
end

.license_filesObject



39
40
41
# File 'lib/licensee/license.rb', line 39

def license_files
  @license_files ||= Dir.glob("#{license_dir}/*.txt")
end

Instance Method Details

#==(other) ⇒ Object



122
123
124
# File 'lib/licensee/license.rb', line 122

def ==(other)
  other != nil && key == other.key
end

#contentObject Also known as: to_s, text, body

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



107
108
109
# File 'lib/licensee/license.rb', line 107

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

#featured?Boolean Also known as: featured

Returns:

  • (Boolean)


96
97
98
# File 'lib/licensee/license.rb', line 96

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

#hidden?Boolean

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/licensee/license.rb', line 101

def hidden?
  return true if HIDDEN_LICENSES.include?(key)
  !!(meta["hidden"] if meta)
end

#inspectObject



114
115
116
# File 'lib/licensee/license.rb', line 114

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

#metaObject

License metadata from YAML front matter



72
73
74
75
76
77
78
79
80
81
# File 'lib/licensee/license.rb', line 72

def meta
  @meta ||= if parts && parts[1]
    if YAML.respond_to? :safe_load
      meta = YAML.safe_load(parts[1])
    else
      meta = YAML.load(parts[1])
    end
    YAML_DEFAULTS.merge(meta)
  end
end

#nameObject

Returns the human-readable license name



84
85
86
# File 'lib/licensee/license.rb', line 84

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

#name_without_versionObject



92
93
94
# File 'lib/licensee/license.rb', line 92

def name_without_version
  /(.+?)(( v?\d\.\d)|$)/.match(name)[1]
end

#nicknameObject



88
89
90
# File 'lib/licensee/license.rb', line 88

def nickname
  meta["nickname"] if meta
end

#pathObject

Path to vendored license file on disk



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

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

#urlObject



118
119
120
# File 'lib/licensee/license.rb', line 118

def url
  URI.join(Licensee::DOMAIN, "/licenses/#{key}/").to_s
end