licensee 9.0.0 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9511aa5acc8806c546383c7c3a9768e604c303fe
4
- data.tar.gz: 6ac4084aece7db23441894eaa7832d4fa9c5cb44
3
+ metadata.gz: ab35a217757c184a6a0b91d6536c8ab7a8b19c44
4
+ data.tar.gz: 9cdabe6b6e388c2ef893f8e691aa1aa5aa7e78b2
5
5
  SHA512:
6
- metadata.gz: ac982657202244e0460648cca93b7877e802aae30ba6369c0fcb12d2ae6f76056473ac0c8a5eaa85f43c2101e23d691286eadaab9e1dcd39112edcff4c978064
7
- data.tar.gz: 4873e3d6dcb1e6285d4b4c41177be741f3ffc4b7ae2678eedb44ca5581074a95703dc33dcfb32b0587df3e251cbf00ba59f603b46e7b3f59270a1deb14f59a99
6
+ metadata.gz: 41e4cff0f46f818a9cacfc83b7e0bb495b59152e29e9a95a12a9296e0a87d0b051ac009c329ef435804051076281724ece0c18612ebe7e38a53ed230830f7466
7
+ data.tar.gz: d69564bffdc75c6af44a36795d7cee2c5dddf8b91cb65ce92e1eb3345ecc31500c31a2a2a47b828c0476181356a39c76c00a0e6f94775a3b1e27224a7d55d9ce
@@ -1,5 +1,4 @@
1
1
  require 'uri'
2
- require 'yaml'
3
2
 
4
3
  module Licensee
5
4
  class InvalidLicense < ArgumentError; end
@@ -60,11 +59,8 @@ module Licensee
60
59
 
61
60
  attr_reader :key
62
61
 
63
- # These should be in sync with choosealicense.com's collection defaults
64
- YAML_DEFAULTS = {
65
- 'featured' => false,
66
- 'hidden' => true
67
- }.freeze
62
+ # Preserved for backwards compatability
63
+ YAML_DEFAULTS = Licensee::LicenseMeta.members
68
64
 
69
65
  # Pseudo-license are license placeholders with no content
70
66
  #
@@ -75,6 +71,8 @@ module Licensee
75
71
  PSEUDO_LICENSES = %w[other no-license].freeze
76
72
 
77
73
  include Licensee::ContentHelper
74
+ extend Forwardable
75
+ def_delegators :meta, *LicenseMeta.helper_methods
78
76
 
79
77
  def initialize(key)
80
78
  @key = key.downcase
@@ -87,33 +85,20 @@ module Licensee
87
85
 
88
86
  # License metadata from YAML front matter with defaults merged in
89
87
  def meta
90
- @meta ||= begin
91
- return YAML_DEFAULTS unless parts && parts[1]
92
- meta = YAML.safe_load(parts[1])
93
- YAML_DEFAULTS.merge(meta)
94
- end
88
+ @meta ||= LicenseMeta.from_yaml(yaml)
95
89
  end
96
90
 
97
91
  # Returns the human-readable license name
98
92
  def name
99
- meta['title'] ? meta['title'] : key.capitalize
100
- end
101
-
102
- def nickname
103
- meta['nickname']
93
+ title ? title : key.capitalize
104
94
  end
105
95
 
106
96
  def name_without_version
107
97
  /(.+?)(( v?\d\.\d)|$)/.match(name)[1]
108
98
  end
109
99
 
110
- def featured?
111
- meta['featured']
112
- end
113
- alias featured featured?
114
-
115
- def hidden?
116
- meta['hidden']
100
+ def other?
101
+ key == 'other'
117
102
  end
118
103
 
119
104
  def gpl?
@@ -183,5 +168,9 @@ module Licensee
183
168
  return unless raw_content
184
169
  @parts ||= raw_content.match(/\A(---\n.*\n---\n+)?(.*)/m).to_a
185
170
  end
171
+
172
+ def yaml
173
+ @yaml ||= parts[1] if parts
174
+ end
186
175
  end
187
176
  end
@@ -0,0 +1,56 @@
1
+ require 'yaml'
2
+
3
+ module Licensee
4
+ class LicenseMeta < Struct.new(
5
+ :title, :spdx_id, :source, :description, :how, :conditions, :permissions,
6
+ :limitations, :using, :featured, :hidden, :nickname, :note
7
+ )
8
+
9
+ # These should be in sync with choosealicense.com's collection defaults
10
+ DEFAULTS = {
11
+ 'featured' => false,
12
+ 'hidden' => true
13
+ }.freeze
14
+
15
+ PREDICATE_FIELDS = %i[featured hidden].freeze
16
+
17
+ class << self
18
+ # Create a new LicenseMeta from YAML
19
+ #
20
+ # yaml - the raw YAML string
21
+ #
22
+ # returns a LicenseMeta with defaults set
23
+ def from_yaml(yaml)
24
+ return from_hash({}) if yaml.nil? || yaml.to_s.empty?
25
+ from_hash YAML.safe_load(yaml)
26
+ end
27
+
28
+ # Create a new LicenseMeta from a hash
29
+ #
30
+ # hash - the hash of key/value meta pairs
31
+ #
32
+ # returns a LicenseMeta with defaults set
33
+ def from_hash(hash)
34
+ hash = DEFAULTS.merge(hash)
35
+ hash['spdx_id'] = hash.delete('spdx-id')
36
+ ordered_array = hash.values_at(*members.map(&:to_s))
37
+ new(*ordered_array)
38
+ end
39
+
40
+ # Array of symbolized helper methods to expose on the License class
41
+ def helper_methods
42
+ members - PREDICATE_FIELDS + PREDICATE_FIELDS.map { |f| "#{f}?".to_sym }
43
+ end
44
+ end
45
+
46
+ PREDICATE_FIELDS.each do |field|
47
+ alias_method "#{field}?".to_sym, field
48
+ end
49
+
50
+ # Backward compatibalize `#["spdx-id"]` calls to avoid a breaking change
51
+ def [](key)
52
+ key = 'spdx_id' if key == 'spdx-id'
53
+ super(key)
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module Licensee
2
- VERSION = '9.0.0'.freeze
2
+ VERSION = '9.1.0'.freeze
3
3
  end
data/lib/licensee.rb CHANGED
@@ -6,6 +6,7 @@ require 'rugged'
6
6
  module Licensee
7
7
  autoload :ContentHelper, 'licensee/content_helper'
8
8
  autoload :License, 'licensee/license'
9
+ autoload :LicenseMeta, 'licensee/license_meta'
9
10
  autoload :Rule, 'licensee/rule'
10
11
  autoload :Matchers, 'licensee/matchers'
11
12
  autoload :Projects, 'licensee/projects'
@@ -0,0 +1,110 @@
1
+ RSpec.describe Licensee::LicenseMeta do
2
+ subject { Licensee::License.find('mit').meta }
3
+
4
+ meta_fields.each do |field|
5
+ next if field['name'] == 'redirect_from'
6
+
7
+ context "the #{field['name']} field" do
8
+ let(:name) { field['name'].tr('-', '_') }
9
+ let(:method) { name.to_sym }
10
+
11
+ it 'responds to the field as a method' do
12
+ expect(subject).to respond_to(method)
13
+ end
14
+
15
+ it 'responds to the field as a hash key' do
16
+ if field['required']
17
+ expect(subject[name]).to_not be_nil
18
+ else
19
+ expect { subject[name] }.not_to raise_error
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ context 'predicate methods' do
26
+ described_class::PREDICATE_FIELDS.each do |field|
27
+ context "the #{field}? method" do
28
+ it 'responds' do
29
+ expect(subject).to respond_to("#{field}?".to_sym)
30
+ end
31
+
32
+ it 'is boolean' do
33
+ expect(subject.send("#{field}?".to_sym)).to be(true).or be(false)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ context '#from_hash' do
40
+ let(:hash) do
41
+ { 'title' => 'Test license', 'description' => 'A test license' }
42
+ end
43
+ subject { described_class.from_hash(hash) }
44
+
45
+ it 'sets values' do
46
+ expect(subject.title).to eql('Test license')
47
+ expect(subject.description).to eql('A test license')
48
+ end
49
+
50
+ context 'setting defaults' do
51
+ let(:hash) { {} }
52
+
53
+ described_class::DEFAULTS.each do |key, value|
54
+ it "sets the #{key} field to #{value}" do
55
+ expect(subject[key]).to eql(value)
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'spdx-id' do
61
+ let(:hash) { { 'spdx-id' => 'foo' } }
62
+
63
+ it 'renames spdx-id to spdx_id' do
64
+ expect(subject['spdx_id']).to eql('foo')
65
+ end
66
+
67
+ it 'exposes spdx-id via #[]' do
68
+ expect(subject['spdx-id']).to eql('foo')
69
+ end
70
+ end
71
+ end
72
+
73
+ context '#from_yaml' do
74
+ let(:yaml) { "title: Test license\ndescription: A test license" }
75
+ subject { described_class.from_yaml(yaml) }
76
+
77
+ it 'parses yaml' do
78
+ expect(subject.title).to eql('Test license')
79
+ expect(subject.description).to eql('A test license')
80
+ end
81
+
82
+ it 'sets defaults' do
83
+ expect(subject.hidden).to eql(true)
84
+ expect(subject.featured).to eql(false)
85
+ end
86
+
87
+ context 'nil yaml' do
88
+ let(:yaml) { nil }
89
+
90
+ it 'returns defaults' do
91
+ expect(subject.hidden).to eql(true)
92
+ end
93
+ end
94
+
95
+ context 'empty yaml' do
96
+ let(:yaml) { '' }
97
+
98
+ it 'returns defaults' do
99
+ expect(subject.featured).to eql(false)
100
+ end
101
+ end
102
+ end
103
+
104
+ it 'returns the list of helper methods' do
105
+ expect(described_class.helper_methods.length).to eql(13)
106
+ expect(described_class.helper_methods).to include(:hidden?)
107
+ expect(described_class.helper_methods).to_not include(:hidden)
108
+ expect(described_class.helper_methods).to include(:title)
109
+ end
110
+ end
@@ -127,11 +127,26 @@ RSpec.describe Licensee::License do
127
127
  expect(mit.key).to eql('mit')
128
128
  end
129
129
 
130
+ it 'exposes the SPDX ID' do
131
+ expect(gpl.spdx_id).to eql('GPL-3.0')
132
+ end
133
+
134
+ context '#other?' do
135
+ it 'knows MIT is not other' do
136
+ expect(gpl).to_not be_other
137
+ end
138
+
139
+ it 'knows the other license is other?' do
140
+ expect(other).to be_other
141
+ end
142
+ end
143
+
130
144
  context 'meta' do
131
145
  it 'exposes license meta' do
132
146
  expect(mit).to respond_to(:meta)
133
- expect(mit.meta).to have_key('title')
147
+ expect(mit.meta).to respond_to(:title)
134
148
  expect(mit.meta['title']).to eql('MIT License')
149
+ expect(mit.meta.title).to eql('MIT License')
135
150
  end
136
151
 
137
152
  it 'includes defaults' do
data/spec/spec_helper.rb CHANGED
@@ -66,6 +66,12 @@ def format_percent(float)
66
66
  "#{format('%.2f', float)}%"
67
67
  end
68
68
 
69
+ def meta_fields
70
+ path = 'vendor/choosealicense.com/_data/meta.yml'
71
+ path = File.expand_path(path, project_root)
72
+ YAML.safe_load(File.read(path))
73
+ end
74
+
69
75
  RSpec::Matchers.define :be_an_existing_file do
70
76
  match { |path| File.exist?(path) }
71
77
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: licensee
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.0.0
4
+ version: 9.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Balter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-25 00:00:00.000000000 Z
11
+ date: 2017-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rugged
@@ -108,6 +108,7 @@ files:
108
108
  - lib/licensee.rb
109
109
  - lib/licensee/content_helper.rb
110
110
  - lib/licensee/license.rb
111
+ - lib/licensee/license_meta.rb
111
112
  - lib/licensee/matchers.rb
112
113
  - lib/licensee/matchers/cabal.rb
113
114
  - lib/licensee/matchers/copyright.rb
@@ -156,6 +157,7 @@ files:
156
157
  - spec/fixtures/wrk-modified-apache/LICENSE
157
158
  - spec/integration_spec.rb
158
159
  - spec/licensee/content_helper_spec.rb
160
+ - spec/licensee/license_meta_spec.rb
159
161
  - spec/licensee/license_spec.rb
160
162
  - spec/licensee/matchers/cabal_matcher_spec.rb
161
163
  - spec/licensee/matchers/copyright_matcher_spec.rb