Class: PDK::Validate::ValidatorGroup Abstract

Inherits:
Validator
  • Object
show all
Defined in:
lib/pdk/validate/validator_group.rb

Overview

This class is abstract.

The base class which should be used by meta-validators, that is, this group executes other validators

At a minimum, the ‘name` and `validators` methods should be overridden in the child class

An example concrete implementation could look like:

module PDK

module Validate
  module Tasks
    class TasksValidatorGroup < ValidatorGroup
      def name
        'tasks'
      end

      def validators
        [
          TasksNameValidator,
          TasksMetadataLintValidator,
        ].freeze
      end
    end
  end
end

end

See Also:

Instance Attribute Summary

Attributes inherited from Validator

#context, #options, #prepared

Instance Method Summary collapse

Methods inherited from Validator

#initialize, #name, #spinners_enabled?, #start_spinner, #stop_spinner, #valid_in_context?

Constructor Details

This class inherits a constructor from PDK::Validate::Validator

Instance Method Details

#invoke(report) ⇒ Object

See Also:

  • PDK::Validate::Validator.invoke


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pdk/validate/validator_group.rb', line 82

def invoke(report)
  exit_code = 0

  prepare_invoke!
  start_spinner

  validator_instances.each do |instance|
    exit_code = instance.invoke(report)
    break if exit_code != 0
  end

  stop_spinner(exit_code.zero?)

  exit_code
end

#prepare_invoke!Object

Can be overridden by child classes to do their own preparation tasks. Typically this is not required by a meta-validator though.

See Also:

  • PDK::Validate::Validator.prepare_invoke!


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pdk/validate/validator_group.rb', line 61

def prepare_invoke!
  return if @prepared

  super

  # Force the spinner to be registered etc.
  spinner

  # Prepare child validators
  validator_instances.each(&:prepare_invoke!)
  nil
end

#spinnerObject

See Also:

  • PDK::Validate::Validator.spinner


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pdk/validate/validator_group.rb', line 39

def spinner
  return nil unless spinners_enabled?
  return @spinner unless @spinner.nil?

  require 'pdk/cli/util/spinner'

  @spinner = TTY::Spinner::Multi.new("[:spinner] #{spinner_text}", PDK::CLI::Util.spinner_opts_for_platform)

  # Register the child spinners
  validator_instances.each do |instance|
    next if instance.spinner.nil?

    @spinner.register(instance.spinner)
  end

  @spinner
end

#spinner_textObject

See Also:

  • PDK::Validate::Validator.spinner_text


34
35
36
# File 'lib/pdk/validate/validator_group.rb', line 34

def spinner_text
  format('Running %{name} validators ...', name: name)
end

#validator_instancesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The instanitated PDK::Validator::Validator classes from the ‘validators` array



101
102
103
104
# File 'lib/pdk/validate/validator_group.rb', line 101

def validator_instances
  @validator_instances ||= validators.map { |klass| klass.new(context, options.merge(parent_validator: self)) }
                                     .select(&:valid_in_context?)
end

#validatorsObject

This method is abstract.

A list of Validator classes that this group will run



77
78
79
# File 'lib/pdk/validate/validator_group.rb', line 77

def validators
  []
end