Class: LicenseFinder::Decisions

Inherits:
Object
  • Object
show all
Defined in:
lib/license_finder/decisions.rb

Defined Under Namespace

Classes: TXN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDecisions



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/license_finder/decisions.rb', line 67

def initialize
  @decisions = []
  @packages = Set.new
  @licenses = Hash.new { |h, k| h[k] = Set.new }
  @homepages = {}
  @approvals = {}
  @permitted = Set.new
  @restricted = Set.new
  @ignored = Set.new
  @ignored_groups = Set.new
  @inherited_decisions = Set.new
end

Instance Attribute Details

#ignoredObject (readonly)

READ



11
12
13
# File 'lib/license_finder/decisions.rb', line 11

def ignored
  @ignored
end

#ignored_groupsObject (readonly)

READ



11
12
13
# File 'lib/license_finder/decisions.rb', line 11

def ignored_groups
  @ignored_groups
end

#inherited_decisionsObject (readonly)

READ



11
12
13
# File 'lib/license_finder/decisions.rb', line 11

def inherited_decisions
  @inherited_decisions
end

#packagesObject (readonly)

READ



11
12
13
# File 'lib/license_finder/decisions.rb', line 11

def packages
  @packages
end

#permittedObject (readonly)

READ



11
12
13
# File 'lib/license_finder/decisions.rb', line 11

def permitted
  @permitted
end

#project_nameObject (readonly)

READ



11
12
13
# File 'lib/license_finder/decisions.rb', line 11

def project_name
  @project_name
end

#restrictedObject (readonly)

READ



11
12
13
# File 'lib/license_finder/decisions.rb', line 11

def restricted
  @restricted
end

Class Method Details

.fetch_saved(file) ⇒ Object

PERSIST



231
232
233
# File 'lib/license_finder/decisions.rb', line 231

def self.fetch_saved(file)
  restore(read!(file))
end

.read!(file) ⇒ Object



253
254
255
# File 'lib/license_finder/decisions.rb', line 253

def self.read!(file)
  file.read if file.exist?
end

.restore(persisted, result = new) ⇒ Object



239
240
241
242
243
244
245
246
247
# File 'lib/license_finder/decisions.rb', line 239

def self.restore(persisted, result = new)
  return result unless persisted

  actions = YAML.load(persisted)
  (actions || []).each do |action, *args|
    result.send(action, *args)
  end
  result
end

Instance Method Details

#add_decision(decision) ⇒ Object



205
206
207
# File 'lib/license_finder/decisions.rb', line 205

def add_decision(decision)
  @decisions << decision unless @inherited
end

#add_package(name, version, txn = {}) ⇒ Object



80
81
82
83
84
# File 'lib/license_finder/decisions.rb', line 80

def add_package(name, version, txn = {})
  add_decision [:add_package, name, version, txn]
  @packages << ManualPackage.new(name, version)
  self
end

#approval_of(name, version = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/license_finder/decisions.rb', line 21

def approval_of(name, version = nil)
  if !@approvals.key?(name)
    nil
  elsif !version.nil?
    @approvals[name] if @approvals[name][:safe_versions].empty? || @approvals[name][:safe_versions].include?(version)
  elsif @approvals[name][:safe_versions].empty?
    @approvals[name]
  end
end

#approve(name, txn = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/license_finder/decisions.rb', line 110

def approve(name, txn = {})
  add_decision [:approve, name, txn]

  versions = []
  versions = @approvals[name][:safe_versions] if @approvals.key?(name)
  @approvals[name] = TXN.from_hash(txn, versions)
  @approvals[name][:safe_versions].concat(txn[:versions]) unless txn[:versions].nil?
  self
end

#approved?(name, version = nil) ⇒ Boolean



31
32
33
34
35
36
37
38
39
# File 'lib/license_finder/decisions.rb', line 31

def approved?(name, version = nil)
  if !@approvals.key?(name)
    nil
  elsif !version.nil?
    @approvals.key?(name) && @approvals[name][:safe_versions].empty? || @approvals[name][:safe_versions].include?(version)
  else
    @approvals.key?(name)
  end
end

#heed(name, txn = {}) ⇒ Object



156
157
158
159
160
# File 'lib/license_finder/decisions.rb', line 156

def heed(name, txn = {})
  add_decision [:heed, name, txn]
  @ignored.delete(name)
  self
end

#heed_group(name, txn = {}) ⇒ Object



168
169
170
171
172
# File 'lib/license_finder/decisions.rb', line 168

def heed_group(name, txn = {})
  add_decision [:heed_group, name, txn]
  @ignored_groups.delete(name)
  self
end

#homepage(name, homepage, txn = {}) ⇒ Object



104
105
106
107
108
# File 'lib/license_finder/decisions.rb', line 104

def homepage(name, homepage, txn = {})
  add_decision [:homepage, name, homepage, txn]
  @homepages[name] = homepage
  self
end

#homepage_of(name) ⇒ Object



17
18
19
# File 'lib/license_finder/decisions.rb', line 17

def homepage_of(name)
  @homepages[name]
end

#ignore(name, txn = {}) ⇒ Object



150
151
152
153
154
# File 'lib/license_finder/decisions.rb', line 150

def ignore(name, txn = {})
  add_decision [:ignore, name, txn]
  @ignored << name
  self
end

#ignore_group(name, txn = {}) ⇒ Object



162
163
164
165
166
# File 'lib/license_finder/decisions.rb', line 162

def ignore_group(name, txn = {})
  add_decision [:ignore_group, name, txn]
  @ignored_groups << name
  self
end

#ignored?(name) ⇒ Boolean



49
50
51
# File 'lib/license_finder/decisions.rb', line 49

def ignored?(name)
  @ignored.include?(name)
end

#ignored_group?(name) ⇒ Boolean



53
54
55
# File 'lib/license_finder/decisions.rb', line 53

def ignored_group?(name)
  @ignored_groups.include?(name)
end

#inherit_from(filepath) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/license_finder/decisions.rb', line 186

def inherit_from(filepath)
  decisions =
    if filepath =~ %r{^https?://}
      open_uri(filepath).read
    else
      Pathname(filepath).read
    end

  add_decision [:inherit_from, filepath]
  @inherited_decisions << filepath
  restore_inheritance(decisions)
end

#license(name, lic, txn = {}) ⇒ Object



92
93
94
95
96
# File 'lib/license_finder/decisions.rb', line 92

def license(name, lic, txn = {})
  add_decision [:license, name, lic, txn]
  @licenses[name] << License.find_by_name(lic)
  self
end

#licenses_of(name) ⇒ Object



13
14
15
# File 'lib/license_finder/decisions.rb', line 13

def licenses_of(name)
  @licenses[name]
end

#name_project(name, txn = {}) ⇒ Object



174
175
176
177
178
# File 'lib/license_finder/decisions.rb', line 174

def name_project(name, txn = {})
  add_decision [:name_project, name, txn]
  @project_name = name
  self
end

#open_uri(uri) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/license_finder/decisions.rb', line 216

def open_uri(uri)
  # ruby < 2.5.0 URI.open is private
  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0')
    # rubocop:disable Security/Open
    open(uri)
    # rubocop:enable Security/Open
  else
    URI.open(uri)
  end
end

#permit(lic, txn = {}) ⇒ Object



126
127
128
129
130
# File 'lib/license_finder/decisions.rb', line 126

def permit(lic, txn = {})
  add_decision [:permit, lic, txn]
  @permitted << License.find_by_name(lic)
  self
end

#permitted?(lic) ⇒ Boolean



41
42
43
# File 'lib/license_finder/decisions.rb', line 41

def permitted?(lic)
  @permitted.include?(lic)
end

#persistObject



249
250
251
# File 'lib/license_finder/decisions.rb', line 249

def persist
  YAML.dump(@decisions)
end

#remove_inheritance(filepath) ⇒ Object



199
200
201
202
203
# File 'lib/license_finder/decisions.rb', line 199

def remove_inheritance(filepath)
  @decisions -= [[:inherit_from, filepath]]
  @inherited_decisions.delete(filepath)
  self
end

#remove_package(name, txn = {}) ⇒ Object



86
87
88
89
90
# File 'lib/license_finder/decisions.rb', line 86

def remove_package(name, txn = {})
  add_decision [:remove_package, name, txn]
  @packages.delete(ManualPackage.new(name))
  self
end

#restore_inheritance(decisions) ⇒ Object



209
210
211
212
213
214
# File 'lib/license_finder/decisions.rb', line 209

def restore_inheritance(decisions)
  @inherited = true
  self.class.restore(decisions, self)
  @inherited = false
  self
end

#restrict(lic, txn = {}) ⇒ Object



138
139
140
141
142
# File 'lib/license_finder/decisions.rb', line 138

def restrict(lic, txn = {})
  add_decision [:restrict, lic, txn]
  @restricted << License.find_by_name(lic)
  self
end

#restricted?(lic) ⇒ Boolean



45
46
47
# File 'lib/license_finder/decisions.rb', line 45

def restricted?(lic)
  @restricted.include?(lic)
end

#save!(file) ⇒ Object



235
236
237
# File 'lib/license_finder/decisions.rb', line 235

def save!(file)
  write!(persist, file)
end

#unapprove(name, txn = {}) ⇒ Object



120
121
122
123
124
# File 'lib/license_finder/decisions.rb', line 120

def unapprove(name, txn = {})
  add_decision [:unapprove, name, txn]
  @approvals.delete(name)
  self
end

#unlicense(name, lic, txn = {}) ⇒ Object



98
99
100
101
102
# File 'lib/license_finder/decisions.rb', line 98

def unlicense(name, lic, txn = {})
  add_decision [:unlicense, name, lic, txn]
  @licenses[name].delete(License.find_by_name(lic))
  self
end

#unname_project(txn = {}) ⇒ Object



180
181
182
183
184
# File 'lib/license_finder/decisions.rb', line 180

def unname_project(txn = {})
  add_decision [:unname_project, txn]
  @project_name = nil
  self
end

#unpermit(lic, txn = {}) ⇒ Object



132
133
134
135
136
# File 'lib/license_finder/decisions.rb', line 132

def unpermit(lic, txn = {})
  add_decision [:unpermit, lic, txn]
  @permitted.delete(License.find_by_name(lic))
  self
end

#unrestrict(lic, txn = {}) ⇒ Object



144
145
146
147
148
# File 'lib/license_finder/decisions.rb', line 144

def unrestrict(lic, txn = {})
  add_decision [:unrestrict, lic, txn]
  @restricted.delete(License.find_by_name(lic))
  self
end

#write!(value, file) ⇒ Object



257
258
259
260
261
262
# File 'lib/license_finder/decisions.rb', line 257

def write!(value, file)
  file.dirname.mkpath
  file.open('w+') do |f|
    f.print value
  end
end