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
}.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, ContentHelper::END_OF_TERMS_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContentHelper

#content_normalized, #hash, #length, #length_delta, #max_delta, #similarity, #wordset

Constructor Details

#initialize(key) ⇒ License

Returns a new instance of License.



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

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



131
132
133
# File 'lib/licensee/license.rb', line 131

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

#contentObject Also known as: to_s, text, body

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



120
121
122
# File 'lib/licensee/license.rb', line 120

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

#creative_commons?Boolean

Is this license a Creative Commons license?

Returns:

  • (Boolean)


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

def creative_commons?
  key.start_with?('cc-')
end

#featured?Boolean Also known as: featured

Returns:

  • (Boolean)


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

def featured?
  meta['featured']
end

#gpl?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/licensee/license.rb', line 110

def gpl?
  key == 'gpl-2.0' || key == 'gpl-3.0'
end

#hidden?Boolean

Returns:

  • (Boolean)


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

def hidden?
  meta['hidden']
end

#inspectObject



152
153
154
# File 'lib/licensee/license.rb', line 152

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

#metaObject

License metadata from YAML front matter with defaults merged in



80
81
82
83
84
85
86
# File 'lib/licensee/license.rb', line 80

def meta
  @meta ||= begin
    return YAML_DEFAULTS unless parts && parts[1]
    meta = YAML.safe_load(parts[1])
    YAML_DEFAULTS.merge(meta)
  end
end

#nameObject

Returns the human-readable license name



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

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

#name_without_versionObject



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

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

#nicknameObject



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

def nickname
  meta['nickname']
end

#pathObject

Path to vendored license file on disk



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

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

#pseudo_license?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/licensee/license.rb', line 135

def pseudo_license?
  PSEUDO_LICENSES.include?(key)
end

#rulesObject

Returns a hash in the form of rule_group => rules describing what you legally can and can’t do with the given license



141
142
143
144
145
146
147
148
149
150
# File 'lib/licensee/license.rb', line 141

def rules
  return @rules if defined? @rules
  @rules = {}

  Rule.groups.each do |group|
    @rules[group] = meta[group].map { |tag| Rule.find_by_tag(tag) }
  end

  @rules
end

#urlObject



127
128
129
# File 'lib/licensee/license.rb', line 127

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