Class: LicenseFinder::ManualLicenses

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

Instance Method Summary collapse

Constructor Details

#initializeManualLicenses

Returns a new instance of ManualLicenses.



5
6
7
8
# File 'lib/license_finder/manual_licenses.rb', line 5

def initialize
  @all_versions = {}
  @specific_versions = {}
end

Instance Method Details

#assign_to_all_versions(name, lic) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/license_finder/manual_licenses.rb', line 20

def assign_to_all_versions(name, lic)
  # Ex: licenses add foo_gem MIT => Adds MIT at "all" versions for this gem

  @all_versions[name] ||= Set.new
  @all_versions[name] << to_license(lic)

  @specific_versions.delete(name)
end

#assign_to_specific_versions(name, lic, versions) ⇒ Object



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

def assign_to_specific_versions(name, lic, versions)
  # Ex: licenses add foo_gem MIT --version=1.0 => Adds MIT at only 1.0 for this gem

  @specific_versions[name] ||= {}
  versions.each do |version|
    @specific_versions[name][version] ||= Set.new
    @specific_versions[name][version] << to_license(lic)
  end

  @all_versions.delete(name)
end

#licenses_of(name, version = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/license_finder/manual_licenses.rb', line 10

def licenses_of(name, version = nil)
  return @all_versions[name] if @all_versions[name]

  if version && @specific_versions[name] && @specific_versions[name][version]
    @specific_versions[name][version]
  else
    Set.new
  end
end

#unassign_from_all_versions(name, lic = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/license_finder/manual_licenses.rb', line 41

def unassign_from_all_versions(name, lic = nil)
  if lic
    # Ex: licenses remove foo_gem MIT => Removes MIT at all versions for this gem
    @all_versions[name]&.delete(to_license(lic))

    @specific_versions[name]&.each_value do |licenses|
      licenses.delete(to_license(lic))
    end
  else
    # Ex: licenses remove foo_gem => Removes all licenses for all versions of the gem
    @all_versions.delete(name)
    @specific_versions.delete(name)
  end
end

#unassign_from_specific_versions(name, lic, versions) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/license_finder/manual_licenses.rb', line 56

def unassign_from_specific_versions(name, lic, versions)
  return unless @specific_versions[name]

  versions.each do |version|
    if @specific_versions[name][version]
      if lic
        # Ex: licenses remove foo_gem MIT --version=1.0 => Removes MIT at only 1.0 for this gem
        @specific_versions[name][version].delete(to_license(lic))
        @specific_versions[name].delete(version) if @specific_versions[name][version].empty?
      else
        # Ex: licenses remove foo_gem --version=1.0 => Removes all licenses at only 1.0 for the gem
        @specific_versions[name].delete(version)
      end
    end
  end
end