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

93 lines
2 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)
2019-12-26 22:10:19 +05:30
attr_reader :entry_class
def initialize(entry_class)
@entry_class = entry_class
2019-02-15 15:39:39 +05:30
@metadata = {}
2019-12-26 22:10:19 +05:30
@attributes = { default: entry_class.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
2019-09-30 21:07:59 +05:30
def description
@attributes[:description]
end
2019-12-26 22:10:19 +05:30
def inherit
@attributes[:inherit]
end
2019-09-30 21:07:59 +05:30
def inheritable?
@attributes[:inherit]
end
def reserved?
@attributes[:reserved]
end
2019-02-15 15:39:39 +05:30
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
2019-12-26 22:10:19 +05:30
fabricate(entry_class, @value)
2019-02-15 15:39:39 +05:30
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-12-26 22:10:19 +05:30
fabricate(entry_class, default)
2019-02-15 15:39:39 +05:30
end
end
2019-12-26 22:10:19 +05:30
def fabricate(entry_class, value = nil)
2021-01-03 14:25:43 +05:30
entry_class.new(value, **@metadata) do |node|
2019-02-15 15:39:39 +05:30
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