Class: Dragnet::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/dragnet/exporter.rb

Overview

The base exporter class, receives an array of test records, an array of errors and an array of file names and exports the results to the given files. (For each file the format is deduced from its file name).

Constant Summary collapse

KNOWN_FORMATS =
{
  'HTML' => { extensions: %w[.html .htm], exporter: Dragnet::Exporters::HTMLExporter },
  'JSON' => { extensions: %w[.json], exporter: Dragnet::Exporters::JSONExporter }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_records:, errors:, repository:, targets:, logger:) ⇒ Exporter

Creates a new instance of the class.

Parameters:

  • test_records (Array<Dragnet::TestRecord>)

    The array of MTRs that should be included in the reports.

  • errors (Array<Hash>)

    An array of Hashes with the data of the MTR files that did not pass the validation process.

  • repository (Dragnet::Repository, Dragnet::MultiRepository)

    The repository where the MTR files and the source code are stored.

  • targets (Array<String>)

    The array of target files. For each of them the format of the export will be deduced from the file's extension.

  • logger (#info, #debug)

    A logger object to use for output.



30
31
32
33
34
35
36
# File 'lib/dragnet/exporter.rb', line 30

def initialize(test_records:, errors:, repository:, targets:, logger:)
  @test_records = test_records
  @errors = errors
  @repository = repository
  @targets = targets
  @logger = logger
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



18
19
20
# File 'lib/dragnet/exporter.rb', line 18

def errors
  @errors
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/dragnet/exporter.rb', line 18

def logger
  @logger
end

#repositoryObject (readonly)

Returns the value of attribute repository.



18
19
20
# File 'lib/dragnet/exporter.rb', line 18

def repository
  @repository
end

#targetsObject (readonly)

Returns the value of attribute targets.



18
19
20
# File 'lib/dragnet/exporter.rb', line 18

def targets
  @targets
end

#test_recordsObject (readonly)

Returns the value of attribute test_records.



18
19
20
# File 'lib/dragnet/exporter.rb', line 18

def test_records
  @test_records
end

Instance Method Details

#exportObject

Starts the export process.

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dragnet/exporter.rb', line 42

def export
  logger.info 'Starting export process...'
  log_target_files

  formats.each do |format, targets|
    exporter = KNOWN_FORMATS.dig(format, :exporter).new(
      test_records: test_records, errors: errors, repository: repository, logger: logger
    )

    text = exporter.export
    write_output(text, format, targets)
  end
end