Sha256: 1ef587525b37d26fa7184aefbf70c242df348a8ae3e2ca23a96cfe01c7c7f425

Contents?: true

Size: 1.83 KB

Versions: 14

Compression:

Stored size: 1.83 KB

Contents

require 'csv'

module LicenseFinder
  class CsvReport < Report
    COMMA_SEP = ','.freeze
    AVAILABLE_COLUMNS = %w[name version authors licenses license_links approved summary description homepage install_path package_manager groups].freeze
    MISSING_DEPENDENCY_TEXT = 'This package is not installed. Please install to determine licenses.'.freeze

    def initialize(dependencies, options)
      super
      options[:columns] ||= %w[name version licenses]
      @columns = Array(options[:columns]) & self.class::AVAILABLE_COLUMNS
    end

    def to_s
      CSV.generate(col_sep: self.class::COMMA_SEP) do |csv|
        sorted_dependencies.each do |s|
          csv << format_dependency(s)
        end
      end
    end

    private

    def format_dependency(dep)
      @columns.map do |column|
        send("format_#{column}", dep)
      end
    end

    def format_name(dep)
      dep.name
    end

    def format_version(dep)
      dep.version
    end

    def format_authors(dep)
      dep.authors.to_s.strip
    end

    def format_homepage(dep)
      dep.homepage
    end

    def format_licenses(dep)
      if dep.missing?
        MISSING_DEPENDENCY_TEXT
      else
        dep.licenses.map(&:name).join(self.class::COMMA_SEP)
      end
    end

    def format_license_links(dep)
      dep.licenses.map(&:url).join(self.class::COMMA_SEP)
    end

    def format_approved(dep)
      dep.approved? ? 'Approved' : 'Not approved'
    end

    def format_summary(dep)
      dep.summary.to_s.strip
    end

    def format_description(dep)
      dep.description.to_s.strip
    end

    def format_install_path(dep)
      dep.install_path
    end

    def format_package_manager(dep)
      dep.package_manager
    end

    def format_groups(dep)
      if dep.groups.nil?
        ''
      else
        dep.groups.join(self.class::COMMA_SEP)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
license_finder-5.5.2 lib/license_finder/reports/csv_report.rb
license_finder-5.5.1 lib/license_finder/reports/csv_report.rb
license_finder-5.5.0 lib/license_finder/reports/csv_report.rb
license_finder-5.4.1 lib/license_finder/reports/csv_report.rb
license_finder-5.4.0 lib/license_finder/reports/csv_report.rb
license_finder-5.3.0 lib/license_finder/reports/csv_report.rb
license_finder-5.2.3 lib/license_finder/reports/csv_report.rb
license_finder-5.2.1 lib/license_finder/reports/csv_report.rb
license_finder-5.2.0 lib/license_finder/reports/csv_report.rb
license_finder-5.1.1 lib/license_finder/reports/csv_report.rb
license_finder-5.1.0 lib/license_finder/reports/csv_report.rb
license_finder-5.0.3 lib/license_finder/reports/csv_report.rb
license_finder-5.0.2 lib/license_finder/reports/csv_report.rb
license_finder-5.0.0 lib/license_finder/reports/csv_report.rb