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

51 lines
1.4 KiB
Ruby
Raw Normal View History

2018-05-09 12:01:36 +05:30
module Gitlab
module Ci
module Variables
class Collection
class Item
def initialize(key:, value:, public: true, file: false)
2018-11-08 19:23:39 +05:30
raise ArgumentError, "`value` must be of type String, while it was: #{value.class}" unless
value.is_a?(String) || value.nil?
2018-05-09 12:01:36 +05:30
@variable = {
key: key, value: value, public: public, file: file
}
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