Class: Gitlab::Ci::Variables::Collection::Item

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/ci/variables/collection/item.rb

Constant Summary collapse

VARIABLES_REGEXP =
/\$\$|%%|\$(?<key>[a-zA-Z_][a-zA-Z0-9_]*)|\${\g<key>?}|%\g<key>%/.freeze.freeze
VARIABLE_REF_CHARS =
%w[$ %].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(key:, value:, public: true, file: false, masked: false, raw: false) ⇒ Item

Returns a new instance of Item.

Raises:

  • (ArgumentError)

13
14
15
16
17
18
# File 'lib/gitlab/ci/variables/collection/item.rb', line 13

def initialize(key:, value:, public: true, file: false, masked: false, raw: false)
  raise ArgumentError, "`#{key}` must be of type String or nil value, while it was: #{value.class}" unless
    value.is_a?(String) || value.nil?

  @variable = { key: key, value: value, public: public, file: file, masked: masked, raw: raw }
end

Class Method Details

.fabricate(resource) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gitlab/ci/variables/collection/item.rb', line 57

def self.fabricate(resource)
  case resource
  when Hash
    self.new(**resource.symbolize_keys)
  when ::Ci::HasVariable
    self.new(**resource.to_runner_variable)
  when self
    resource.dup
  else
    raise ArgumentError, "Unknown `#{resource.class}` variable resource!"
  end
end

.possible_var_reference?(value) ⇒ Boolean

Returns:

  • (Boolean)

70
71
72
73
74
# File 'lib/gitlab/ci/variables/collection/item.rb', line 70

def self.possible_var_reference?(value)
  return unless value

  VARIABLE_REF_CHARS.any? { |symbol| value.include?(symbol) }
end

Instance Method Details

#==(other) ⇒ Object


32
33
34
# File 'lib/gitlab/ci/variables/collection/item.rb', line 32

def ==(other)
  to_runner_variable == self.class.fabricate(other).to_runner_variable
end

#[](key) ⇒ Object


28
29
30
# File 'lib/gitlab/ci/variables/collection/item.rb', line 28

def [](key)
  @variable.fetch(key)
end

#depends_onObject


36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/ci/variables/collection/item.rb', line 36

def depends_on
  strong_memoize(:depends_on) do
    next if raw

    next unless self.class.possible_var_reference?(value)

    value.scan(VARIABLES_REGEXP).filter_map(&:last)
  end
end

#rawObject


24
25
26
# File 'lib/gitlab/ci/variables/collection/item.rb', line 24

def raw
  @variable.fetch(:raw)
end

#to_runner_variableObject

If `file: true` has been provided we expose it, otherwise we don't expose `file` attribute at all (stems from what the runner expects).


51
52
53
54
55
# File 'lib/gitlab/ci/variables/collection/item.rb', line 51

def to_runner_variable
  @variable.reject do |hash_key, hash_value|
    (hash_key == :file || hash_key == :raw) && hash_value == false
  end
end

#to_sObject


76
77
78
79
80
# File 'lib/gitlab/ci/variables/collection/item.rb', line 76

def to_s
  return to_runner_variable.to_s unless depends_on

  "#{to_runner_variable}, depends_on=#{depends_on}"
end

#valueObject


20
21
22
# File 'lib/gitlab/ci/variables/collection/item.rb', line 20

def value
  @variable.fetch(:value)
end