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]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContentHelper

#create_word_set

Constructor Details

#initialize(key) ⇒ License

Returns a new instance of License.



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

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Class Method Details

.all(options = {}) ⇒ Object



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

def all(options={})
  @all ||= keys.map { |key| self.new(key) }
  options[:hidden] ? @all : @all.reject { |l| l.hidden? }
end

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



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

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

.keysObject



14
15
16
# File 'lib/licensee/license.rb', line 14

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

.license_dirObject



26
27
28
# File 'lib/licensee/license.rb', line 26

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

.license_filesObject



30
31
32
# File 'lib/licensee/license.rb', line 30

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

Instance Method Details

#==(other) ⇒ Object



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

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

#bodyObject Also known as: to_s, text

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



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

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

#contentObject

Raw content of license file, including YAML front matter



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

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)


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

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

#hidden?Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



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

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

#metaObject

License metadata from YAML front matter



68
69
70
71
72
73
74
75
76
77
# File 'lib/licensee/license.rb', line 68

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



80
81
82
# File 'lib/licensee/license.rb', line 80

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

#name_without_versionObject



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

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

#nicknameObject



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

def nickname
  meta["nickname"] if meta
end

#pathObject

Path to vendored license file on disk



52
53
54
# File 'lib/licensee/license.rb', line 52

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

#urlObject



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

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

#wordsetObject



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

def wordset
  @wordset ||= create_word_set(body)
end