Class: Licensee::License

Inherits:
Object
  • Object
show all
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,
  'variant'  => false
}.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

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.



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

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

Instance Attribute Details

#keyObject (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(options = {})
  options = { hidden: false, featured: nil }.merge(options)
  output = licenses.dup
  output.reject!(&:hidden?) unless options[:hidden]
  return output if options[:featured].nil?
  output.select { |l| l.featured? == options[: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, options = {})
  options = { hidden: true }.merge(options)
  all(options).find { |license| key.casecmp(license.key).zero? }
end

.keysObject



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_dirObject



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

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

.license_filesObject



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



129
130
131
# File 'lib/licensee/license.rb', line 129

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

#contentObject Also known as: to_s, text, body

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



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

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

#featured?Boolean Also known as: featured

Returns:

  • (Boolean)


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

def featured?
  return YAML_DEFAULTS['featured'] unless meta
  meta['featured']
end

#hidden?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
# File 'lib/licensee/license.rb', line 111

def hidden?
  return true if PSEUDO_LICENSES.include?(key)
  return YAML_DEFAULTS['hidden'] unless meta
  meta['hidden']
end

#metaObject

License metadata from YAML front matter



81
82
83
84
85
86
87
88
89
90
# File 'lib/licensee/license.rb', line 81

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

#nameObject

Returns the human-readable license name



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

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

#name_without_versionObject



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

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

#nicknameObject



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

def nickname
  meta['nickname'] if meta
end

#pathObject

Path to vendored license file on disk



76
77
78
# File 'lib/licensee/license.rb', line 76

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

#urlObject



125
126
127
# File 'lib/licensee/license.rb', line 125

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