Sha256: 48b0d5da40412221e7953d1df4cfe1293255c259b12253dc84715bc2ba67033f

Contents?: true

Size: 1.58 KB

Versions: 16

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Spandx
  module Core
    class Cache
      include Enumerable

      attr_reader :package_manager, :root

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

      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

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

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

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

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

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

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

      private

      def digest_for(name)
        Digest::SHA1.hexdigest(name)
      end

      def key_for(name)
        digest_for(name)[0...2]
      end

      def datafiles
        @datafiles ||= candidate_keys.each_with_object({}) do |key, memo|
          memo[key] = DataFile.new(File.join(root, key, package_manager))
        end
      end

      def candidate_keys
        (0x00..0xFF).map { |x| x.to_s(16).upcase.rjust(2, '0').downcase }
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
spandx-0.18.3 lib/spandx/core/cache.rb
spandx-0.18.2 lib/spandx/core/cache.rb
spandx-0.18.1 lib/spandx/core/cache.rb
spandx-0.18.0 lib/spandx/core/cache.rb
spandx-0.17.0 lib/spandx/core/cache.rb
spandx-0.16.1 lib/spandx/core/cache.rb
spandx-0.16.0 lib/spandx/core/cache.rb
spandx-0.15.1 lib/spandx/core/cache.rb
spandx-0.15.0 lib/spandx/core/cache.rb
spandx-0.14.0 lib/spandx/core/cache.rb
spandx-0.13.5 lib/spandx/core/cache.rb
spandx-0.13.4 lib/spandx/core/cache.rb
spandx-0.13.3 lib/spandx/core/cache.rb
spandx-0.13.2 lib/spandx/core/cache.rb
spandx-0.13.1 lib/spandx/core/cache.rb
spandx-0.13.0 lib/spandx/core/cache.rb