debian-mirror-gitlab/lib/gitlab/ci/variables/collection/item.rb

53 lines
1.5 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
module Gitlab
module Ci
module Variables
class Collection
class Item
2019-05-18 00:54:41 +05:30
def initialize(key:, value:, public: true, file: false, masked: false)
2019-02-15 15:39:39 +05:30
raise ArgumentError, "`#{key}` must be of type String or nil value, while it was: #{value.class}" unless
2018-11-08 19:23:39 +05:30
value.is_a?(String) || value.nil?
2018-05-09 12:01:36 +05:30
@variable = {
2019-05-18 00:54:41 +05:30
key: key, value: value, public: public, file: file, masked: masked
2018-05-09 12:01:36 +05:30
}
end
def [](key)
@variable.fetch(key)
end
def ==(other)
to_runner_variable == self.class.fabricate(other).to_runner_variable
end
##
# If `file: true` has been provided we expose it, otherwise we
# don't expose `file` attribute at all (stems from what the runner
# expects).
#
def to_runner_variable
@variable.reject do |hash_key, hash_value|
hash_key == :file && hash_value == false
end
end
def self.fabricate(resource)
case resource
when Hash
2018-11-18 11:00:15 +05:30
self.new(resource.symbolize_keys)
2018-05-09 12:01:36 +05:30
when ::HasVariable
self.new(resource.to_runner_variable)
when self
resource.dup
else
raise ArgumentError, "Unknown `#{resource.class}` variable resource!"
end
end
end
end
end
end
end