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>%/
VARIABLE_REF_CHARS =
%w[$ %].freeze

Class Method Summary collapse

Instance Method Summary collapse

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gitlab/ci/variables/collection/item.rb', line 65

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)


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

def self.possible_var_reference?(value)
  return unless value

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

Instance Method Details

#==(other) ⇒ Object



41
42
43
# File 'lib/gitlab/ci/variables/collection/item.rb', line 41

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

#[](key) ⇒ Object



37
38
39
# File 'lib/gitlab/ci/variables/collection/item.rb', line 37

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

#depends_onObject



45
46
47
48
49
50
51
52
53
# File 'lib/gitlab/ci/variables/collection/item.rb', line 45

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

#file?Boolean

Returns:

  • (Boolean)


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

def file?
  @variable.fetch(:file)
end

#keyObject



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

def key
  @variable.fetch(:key)
end

#raw?Boolean Also known as: raw

Returns:

  • (Boolean)


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

def raw?
  @variable.fetch(:raw)
end

#to_runner_variableObject

If ‘file: true` or `raw: true` has been provided we expose it, otherwise we don’t expose ‘file` and `raw` attributes at all (stems from what the runner expects).



59
60
61
62
63
# File 'lib/gitlab/ci/variables/collection/item.rb', line 59

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

#to_sObject



84
85
86
87
88
# File 'lib/gitlab/ci/variables/collection/item.rb', line 84

def to_s
  return to_runner_variable.to_s unless depends_on

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

#valueObject



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

def value
  @variable.fetch(:value)
end