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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Item.

Raises:

  • (ArgumentError)

8
9
10
11
12
13
14
15
# File 'lib/gitlab/ci/variables/collection/item.rb', line 8

def initialize(key:, value:, public: true, file: false, masked: 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
  }
end

Class Method Details

.fabricate(resource) ⇒ Object


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

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

Instance Method Details

#==(other) ⇒ Object


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

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

#[](key) ⇒ Object


17
18
19
# File 'lib/gitlab/ci/variables/collection/item.rb', line 17

def [](key)
  @variable.fetch(key)
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).


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

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