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
# 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, _('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



112
113
114
115
# File 'lib/pdk/validate/validator.rb', line 112

def invoke(_report)
  prepare_invoke!
  0
end

#nameString

This method is abstract.

Name of the Validator

Returns:

  • (String)


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

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



103
104
105
# File 'lib/pdk/validate/validator.rb', line 103

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:



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

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)


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

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)


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

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



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

def start_spinner
  spinner.auto_spin unless spinner.nil?
  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



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

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)


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

def valid_in_context?
  true
end