debian-mirror-gitlab/lib/gitlab/ci/config/node/factory.rb

74 lines
1.7 KiB
Ruby
Raw Normal View History

module Gitlab
module Ci
class Config
module Node
##
# Factory class responsible for fabricating node entry objects.
#
class Factory
class InvalidFactory < StandardError; end
2016-08-24 12:49:21 +05:30
def initialize(node)
@node = node
2016-09-13 17:45:13 +05:30
@metadata = {}
@attributes = {}
end
2016-09-13 17:45:13 +05:30
def value(value)
@value = value
self
end
def metadata(metadata)
@metadata.merge!(metadata)
self
end
def with(attributes)
@attributes.merge!(attributes)
self
end
def create!
2016-09-13 17:45:13 +05:30
raise InvalidFactory unless defined?(@value)
2016-09-13 17:45:13 +05:30
##
# We assume that unspecified entry is undefined.
# See issue #18775.
#
if @value.nil?
2016-09-29 09:46:39 +05:30
Node::Unspecified.new(
fabricate_unspecified
2016-09-13 17:45:13 +05:30
)
else
fabricate(@node, @value)
end
end
2016-08-24 12:49:21 +05:30
private
2016-09-29 09:46:39 +05:30
def fabricate_unspecified
2016-08-24 12:49:21 +05:30
##
2016-09-13 17:45:13 +05:30
# If node has a default value we fabricate concrete node
# with default value.
2016-08-24 12:49:21 +05:30
#
2016-09-13 17:45:13 +05:30
if @node.default.nil?
2016-09-29 09:46:39 +05:30
fabricate(Node::Undefined)
2016-08-24 12:49:21 +05:30
else
2016-09-13 17:45:13 +05:30
fabricate(@node, @node.default)
end
end
def fabricate(node, value = nil)
node.new(value, @metadata).tap do |entry|
entry.key = @attributes[:key]
entry.parent = @attributes[:parent]
entry.description = @attributes[:description]
2016-08-24 12:49:21 +05:30
end
end
end
end
end
end
end