Class: Spandx::Core::Cache

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/spandx/core/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_manager, root:) ⇒ Cache

Returns a new instance of Cache.



10
11
12
13
# File 'lib/spandx/core/cache.rb', line 10

def initialize(package_manager, root:)
  @package_manager = package_manager.to_s
  @root = root
end

Instance Attribute Details

#package_managerObject (readonly)

Returns the value of attribute package_manager.



8
9
10
# File 'lib/spandx/core/cache.rb', line 8

def package_manager
  @package_manager
end

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/spandx/core/cache.rb', line 8

def root
  @root
end

Instance Method Details

#datafile_for(name) ⇒ Object



42
43
44
# File 'lib/spandx/core/cache.rb', line 42

def datafile_for(name)
  datafiles.fetch(key_for(name))
end

#eachObject



23
24
25
26
27
28
29
# File 'lib/spandx/core/cache.rb', line 23

def each
  datafiles.each do |_hex, datafile|
    datafile.each do |item|
      yield item
    end
  end
end

#insert(name, version, licenses) ⇒ Object



31
32
33
34
35
# File 'lib/spandx/core/cache.rb', line 31

def insert(name, version, licenses)
  return if name.nil? || name.empty?

  datafile_for(name).insert(name, version, licenses)
end

#insert!(*args) ⇒ Object



37
38
39
40
# File 'lib/spandx/core/cache.rb', line 37

def insert!(*args)
  insert(*args)
  rebuild_index
end

#licenses_for(name, version) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/spandx/core/cache.rb', line 15

def licenses_for(name, version)
  return [] if name.nil? || name.empty?

  found = datafile_for(name).search(name: name, version: version)
  Spandx.logger.debug { "Cache miss: #{name}-#{version}" } unless found
  found ? found[2].split('-|-') : []
end

#rebuild_indexObject



46
47
48
49
50
# File 'lib/spandx/core/cache.rb', line 46

def rebuild_index
  datafiles.each do |_hex, datafile|
    datafile.index.update!
  end
end