Class: Ci::Inputs::BaseInput

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
lib/ci/inputs/base_input.rb

Overview

This is a common abstraction for all input types

Direct Known Subclasses

ArrayInput, BooleanInput, NumberInput, StringInput

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, spec:) ⇒ BaseInput

Returns a new instance of BaseInput.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ci/inputs/base_input.rb', line 21

def initialize(name:, spec:)
  @name = name
  @errors = []

  # Treat minimal spec definition (nil) as a valid hash:
  #   spec:
  #     inputs:
  #       website:
  spec_hash = spec || {}
  @spec = spec_hash.is_a?(Hash) ? spec_hash.with_indifferent_access : spec_hash
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/ci/inputs/base_input.rb', line 19

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/ci/inputs/base_input.rb', line 19

def name
  @name
end

#specObject (readonly)

Returns the value of attribute spec.



19
20
21
# File 'lib/ci/inputs/base_input.rb', line 19

def spec
  @spec
end

Class Method Details

.matches?(spec) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/ci/inputs/base_input.rb', line 10

def self.matches?(spec)
  spec.is_a?(Hash) && spec[:type] == type_name
end

.type_nameObject

Human readable type used in error messages

Raises:

  • (NotImplementedError)


15
16
17
# File 'lib/ci/inputs/base_input.rb', line 15

def self.type_name
  raise NotImplementedError
end

Instance Method Details

#actual_value(param, all_params = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ci/inputs/base_input.rb', line 44

def actual_value(param, all_params = {})
  param = nil if param.is_a?(String) && param.empty? && rules

  if param.nil?
    coerced_value(resolved_default(all_params))
  else
    coerced_value(param)
  end
end

#defaultObject



75
76
77
# File 'lib/ci/inputs/base_input.rb', line 75

def default
  spec[:default]
end

#default_value_present?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ci/inputs/base_input.rb', line 71

def default_value_present?
  spec.key?(:default) || has_default_through_rules?
end

#descriptionObject



83
84
85
# File 'lib/ci/inputs/base_input.rb', line 83

def description
  spec[:description]
end

#optionsObject



79
80
81
# File 'lib/ci/inputs/base_input.rb', line 79

def options
  spec[:options]
end

#regexObject



87
88
89
90
91
# File 'lib/ci/inputs/base_input.rb', line 87

def regex
  return unless regex_provided?

  spec[:regex]
end

#required?Boolean

An input specification without a default value is required. For example: “‘yaml spec:

inputs:
  website:

“‘ For rules-based inputs, check if any rule provides a default value (this functionality is under a feature flag).

Returns:

  • (Boolean)


67
68
69
# File 'lib/ci/inputs/base_input.rb', line 67

def required?
  !default_value_present?
end

#rulesObject



93
94
95
96
97
# File 'lib/ci/inputs/base_input.rb', line 93

def rules
  return unless spec.key?(:rules)

  spec[:rules]&.map { |rule| rule.is_a?(Hash) ? rule.with_indifferent_access : rule }
end

#typeObject



54
55
56
# File 'lib/ci/inputs/base_input.rb', line 54

def type
  self.class.type_name
end

#validate_param!(param, all_params = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/ci/inputs/base_input.rb', line 33

def validate_param!(param, all_params = {})
  param = nil if param.is_a?(String) && param.empty? && rules

  error('required value has not been provided') if required? && param.nil?
  return if errors.present?

  run_validations(resolved_default(all_params), all_params, default: true) unless required?

  run_validations(param, all_params) unless param.nil?
end