gitlab-org--gitlab-foss/lib/gitlab/config/entry/factory.rb
Kamil Trzciński 505d71ec88 Introduce default: for gitlab-ci.yml
This moves all existing `image/services/before_script/variables`
into `default:`. This allows us to easily add a default and
top-level entries. `default`: is keep backward compatible: to
be considered to be job if `default:script:` is specified. This
behavior should be removed.

All existing `image/services/before_script/variables` are properly
handled in root context.
2019-06-18 10:36:07 +00:00

86 lines
1.9 KiB
Ruby

# frozen_string_literal: true
module Gitlab
module Config
module Entry
##
# Factory class responsible for fabricating entry objects.
#
class Factory
InvalidFactory = Class.new(StandardError)
def initialize(entry)
@entry = entry
@metadata = {}
@attributes = { default: entry.default }
end
def value(value)
@value = value
self
end
def metadata(metadata)
@metadata.merge!(metadata.compact)
self
end
def with(attributes)
@attributes.merge!(attributes.compact)
self
end
def description
@attributes[:description]
end
def inheritable?
@attributes[:inherit]
end
def reserved?
@attributes[:reserved]
end
def create!
raise InvalidFactory unless defined?(@value)
##
# We assume that unspecified entry is undefined.
# See issue #18775.
#
if @value.nil?
Entry::Unspecified.new(fabricate_unspecified)
else
fabricate(@entry, @value)
end
end
private
def fabricate_unspecified
##
# If entry has a default value we fabricate concrete node
# with default value.
#
default = @attributes.fetch(:default)
if default.nil?
fabricate(Entry::Undefined)
else
fabricate(@entry, default)
end
end
def fabricate(entry, value = nil)
entry.new(value, @metadata) do |node|
node.key = @attributes[:key]
node.parent = @attributes[:parent]
node.default = @attributes[:default]
node.description = @attributes[:description]
end
end
end
end
end
end