Class: PDK::Validate::Validator Abstract

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

Overview

This class is abstract.

The base Validator class which all other validators should inherit from. Acutal validator implementation should inherit from other child abstract classes e.g. ValidatorGroup or ExternalCommandValdiator

Direct Known Subclasses

InvokableValidator, ValidatorGroup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil, options = {}) ⇒ Validator

Creates a new Validator

Parameters:

  • context (PDK::Context::AbstractContext) (defaults to: nil)

    Optional context which specifies where the validation will take place. Passing nil will use a None context (PDK::Context::None)

  • options (Hash) (defaults to: {})

    Optional configuration for the Validator

Options Hash (options):

  • :parent_validator (PDK::Validate::Validator)

    The parent validator for this validator. Typically used by ValidatorGroup to create trees of Validators for invocation.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pdk/validate/validator.rb', line 32

def initialize(context = nil, options = {})
  if context.nil?
    @context = PDK::Context::None.new(nil)
  else
    raise ArgumentError, format('Expected PDK::Context::AbstractContext but got \'%{klass}\' for context', klass: context.class) unless context.is_a?(PDK::Context::AbstractContext)

    @context = context
  end
  @options = options.dup.freeze
  @prepared = false
end

Instance Attribute Details

#contextPDK::Context::AbstractContext (readonly)

The PDK context which the validator will be within.

Returns:



15
16
17
# File 'lib/pdk/validate/validator.rb', line 15

def context
  @context
end

#optionsObject (readonly)

A hash of options set when the Validator was instantiated



11
12
13
# File 'lib/pdk/validate/validator.rb', line 11

def options
  @options
end

#preparedBoolean (readonly)

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.

Whether the validator is prepared to be invoked. This should only be used for testing

Returns:

  • (Boolean)


23
24
25
# File 'lib/pdk/validate/validator.rb', line 23

def prepared
  @prepared
end

Instance Method Details

#invoke(_report) ⇒ Object

This method is abstract.

Invokes the validator and returns the exit code

Parameters:

  • report (PDK::Report)

    Accumulator of events during the invokation of this validator and potential child validators



114
115
116
117
# File 'lib/pdk/validate/validator.rb', line 114

def invoke(_report)
  prepare_invoke!
  0
end

#nameString

This method is abstract.

Name of the Validator

Returns:

  • (String)


98
# File 'lib/pdk/validate/validator.rb', line 98

def name; end

#prepare_invoke!Object

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.

This method is abstract.

Once off tasks to run prior to invoking



105
106
107
# File 'lib/pdk/validate/validator.rb', line 105

def prepare_invoke!
  @prepared = true
end

#spinnerTTY::Spinner?

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.

This method is abstract.

The TTY Spinner for this Validator. Returns nil if spinners are disabled for this validator

Returns:



75
# File 'lib/pdk/validate/validator.rb', line 75

def spinner; end

#spinner_textString

This method is abstract.

Returns the text used for the spinner to display to the user while invoking

Returns:

  • (String)


56
# File 'lib/pdk/validate/validator.rb', line 56

def spinner_text; end

#spinners_enabled?Boolean

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.

Whether Spinners should be enabled for this validator

:nocov: .interactive? is tested elsewhere

Returns:

  • (Boolean)


64
65
66
# File 'lib/pdk/validate/validator.rb', line 64

def spinners_enabled?
  PDK::CLI::Util.interactive?
end

#start_spinnerObject

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.

Start the spinner if it exists



79
80
81
82
# File 'lib/pdk/validate/validator.rb', line 79

def start_spinner
  spinner&.auto_spin
  nil
end

#stop_spinner(success) ⇒ Object

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.

Stop the spinner if it exists



86
87
88
89
90
91
# File 'lib/pdk/validate/validator.rb', line 86

def stop_spinner(success)
  return if spinner.nil?

  success ? spinner.success : spinner.error
  nil
end

#valid_in_context?Boolean

This method is abstract.

Whether this Validator can be invoked in this context. By default any Validator can work in any Context

Returns:

  • (Boolean)


47
48
49
# File 'lib/pdk/validate/validator.rb', line 47

def valid_in_context?
  true
end