Sha256: 444a715de21eddc23d8b485b6daca39198bd87b44b1c4471493bb59398bd88f4

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Spandx
  module Core
    class DataFile
      include Enumerable

      attr_reader :absolute_path

      def initialize(absolute_path)
        @absolute_path = Pathname.new(absolute_path)
        FileUtils.mkdir_p(@absolute_path.dirname)
      end

      def each
        return unless exist?

        open_file(mode: 'rb') do |io|
          while (line = io.gets)
            yield CsvParser.parse(line)
          end
        end
      end

      def search(name:, version:)
        return if name.nil? || name.empty?
        return if version.nil? || name.empty?
        return unless absolute_path.exist?

        term = "#{name}-#{version}"
        index.search do |row|
          term <=> "#{row[0]}-#{row[1]}"
        end
      end

      def insert(name, version, licenses)
        return if [name, version].any? { |x| x.nil? || x.empty? }

        open_file(mode: 'a') do |io|
          io.write(to_csv([name, version, licenses.join('-|-')]))
        end
      end

      def exist?
        absolute_path.exist?
      end

      def open_file(mode: 'rb')
        absolute_path.open(mode) { |io| yield io }
      rescue Errno::ENOENT => error
        Spandx.logger.error(error)
        nil
      end

      def index
        @index ||= IndexFile.new(self)
      end

      private

      def to_csv(array)
        array.to_csv(force_quotes: true)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spandx-0.15.1 lib/spandx/core/data_file.rb
spandx-0.15.0 lib/spandx/core/data_file.rb
spandx-0.14.0 lib/spandx/core/data_file.rb
spandx-0.13.5 lib/spandx/core/data_file.rb
spandx-0.13.4 lib/spandx/core/data_file.rb
spandx-0.13.3 lib/spandx/core/data_file.rb
spandx-0.13.2 lib/spandx/core/data_file.rb
spandx-0.13.1 lib/spandx/core/data_file.rb
spandx-0.13.0 lib/spandx/core/data_file.rb