debian-mirror-gitlab/lib/gitlab/config/entry/factory.rb

75 lines
1.7 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# 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 = {}
2019-03-02 22:35:43 +05:30
@attributes = { default: entry.default }
2019-02-15 15:39:39 +05:30
end
def value(value)
@value = value
self
end
def metadata(metadata)
2019-03-02 22:35:43 +05:30
@metadata.merge!(metadata.compact)
2019-02-15 15:39:39 +05:30
self
end
def with(attributes)
2019-03-02 22:35:43 +05:30
@attributes.merge!(attributes.compact)
2019-02-15 15:39:39 +05:30
self
end
def create!
raise InvalidFactory unless defined?(@value)
##
# We assume that unspecified entry is undefined.
# See issue #18775.
#
if @value.nil?
2019-03-02 22:35:43 +05:30
Entry::Unspecified.new(fabricate_unspecified)
2019-02-15 15:39:39 +05:30
else
fabricate(@entry, @value)
end
end
private
def fabricate_unspecified
##
# If entry has a default value we fabricate concrete node
# with default value.
#
2019-03-02 22:35:43 +05:30
default = @attributes.fetch(:default)
if default.nil?
2019-02-15 15:39:39 +05:30
fabricate(Entry::Undefined)
else
2019-03-02 22:35:43 +05:30
fabricate(@entry, default)
2019-02-15 15:39:39 +05:30
end
end
def fabricate(entry, value = nil)
entry.new(value, @metadata).tap do |node|
node.key = @attributes[:key]
node.parent = @attributes[:parent]
2019-03-02 22:35:43 +05:30
node.default = @attributes[:default]
2019-02-15 15:39:39 +05:30
node.description = @attributes[:description]
end
end
end
end
end
end