Sha256: 6f68496229e4378df7dd2e36c6b94240393fe8e3e519d43e28433c6f15d9b5a3

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Spandx
  module Core
    class Report
      include Enumerable

      FORMATS = {
        csv: :to_csv,
        hash: :to_h,
        json: :to_json,
        table: :to_table,
      }.freeze

      def initialize
        @dependencies = SortedSet.new
      end

      def add(dependency)
        @dependencies << dependency
      end

      def each
        @dependencies.each do |dependency|
          yield dependency
        end
      end

      def to(format, formats: FORMATS)
        public_send(formats.fetch(format&.to_sym, :to_json))
      end

      def to_table
        Table.new do |table|
          map do |dependency|
            table << dependency
          end
        end
      end

      def to_h
        { version: '1.0', dependencies: [] }.tap do |report|
          each do |dependency|
            report[:dependencies].push(dependency.to_h)
          end
        end
      end

      def to_json(*_args)
        JSON.pretty_generate(to_h)
      end

      def to_csv
        map do |dependency|
          CSV.generate_line(dependency.to_a)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spandx-0.13.3 lib/spandx/core/report.rb
spandx-0.13.2 lib/spandx/core/report.rb
spandx-0.13.1 lib/spandx/core/report.rb
spandx-0.13.0 lib/spandx/core/report.rb
spandx-0.12.3 lib/spandx/core/report.rb
spandx-0.12.2 lib/spandx/core/report.rb
spandx-0.12.1 lib/spandx/core/report.rb
spandx-0.12.0 lib/spandx/core/report.rb