Sha256: 50a0f3fab02454bc5cf7b918a7e1595c9ad11d3613c925a7e03fa63bd78e0d9b

Contents?: true

Size: 906 Bytes

Versions: 1

Compression:

Stored size: 906 Bytes

Contents

# frozen_string_literal: true

require 'csv'
require 'json'
require 'site_health/issue'

module SiteHealth
  class IssuesReport
    def initialize(issues)
      @issues = issues
      @fields = Issue.fields
      @select_block = proc { true }
      yield(self) if block_given?
    end

    def fields=(fields)
      @fields = fields.map(&:to_sym)
    end

    def select(&block)
      @select_block = block
    end

    def to_a
      issues = []
      each { |data| issues << data }
      issues
    end

    def to_csv
      CSV.generate do |csv|
        csv << @fields
        each { |data| csv << data.values_at(*@fields) }
      end
    end

    def to_json
      JSON.dump(to_a)
    end

    private

    def each
      @issues.each do |issue|
        next unless @select_block.call(issue)

        hash = issue.to_h.select { |k| @fields.include?(k) }
        yield(hash)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
site_health-0.2.0 lib/site_health/issues_report.rb