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

130 lines
3.6 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Gitlab
module Config
module Entry
##
# This mixin is responsible for adding DSL, which purpose is to
2020-03-13 15:44:24 +05:30
# simplify the process of adding child nodes.
2019-02-15 15:39:39 +05:30
#
# This can be used only if parent node is a configuration entry that
# holds a hash as a configuration value, for example:
#
# job:
# script: ...
# artifacts: ...
#
module Configurable
extend ActiveSupport::Concern
included do
include Validatable
validations do
2019-07-07 11:18:12 +05:30
validates :config, type: Hash, unless: :skip_config_hash_validation?
2019-02-15 15:39:39 +05:30
end
end
def compose!(deps = nil)
return unless valid?
2019-12-26 22:10:19 +05:30
super do
self.class.nodes.each do |key, factory|
# If we override the config type validation
# we can end with different config types like String
next unless config.is_a?(Hash)
2019-07-07 11:18:12 +05:30
2020-01-01 13:55:28 +05:30
entry_create!(key, config[key])
2019-12-26 22:10:19 +05:30
end
2019-02-15 15:39:39 +05:30
2019-12-26 22:10:19 +05:30
yield if block_given?
2019-02-15 15:39:39 +05:30
2019-12-26 22:10:19 +05:30
entries.each_value do |entry|
entry.compose!(deps)
end
2019-02-15 15:39:39 +05:30
end
end
2020-01-01 13:55:28 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def entry_create!(key, value)
factory = self.class
.nodes[key]
.value(value)
.with(key: key, parent: self)
entries[key] = factory.create!
end
2019-02-15 15:39:39 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2019-07-07 11:18:12 +05:30
def skip_config_hash_validation?
false
end
2019-02-15 15:39:39 +05:30
class_methods do
def nodes
2020-07-28 23:09:34 +05:30
return {} unless @nodes
@nodes.transform_values(&:dup)
2019-02-15 15:39:39 +05:30
end
2019-09-30 21:07:59 +05:30
def reserved_node_names
self.nodes.select do |_, node|
node.reserved?
end.keys
end
2019-02-15 15:39:39 +05:30
private
# rubocop: disable CodeReuse/ActiveRecord
2019-12-26 22:10:19 +05:30
def entry(key, entry, description: nil, default: nil, inherit: nil, reserved: nil, metadata: {})
2020-04-08 14:13:33 +05:30
entry_name = key.to_sym
raise ArgumentError, "Entry '#{key}' already defined in '#{name}'" if @nodes.to_h[entry_name]
2019-02-15 15:39:39 +05:30
factory = ::Gitlab::Config::Entry::Factory.new(entry)
2019-09-30 21:07:59 +05:30
.with(description: description)
.with(default: default)
.with(inherit: inherit)
.with(reserved: reserved)
2019-12-26 22:10:19 +05:30
.metadata(metadata)
2019-02-15 15:39:39 +05:30
2020-04-08 14:13:33 +05:30
@nodes ||= {}
@nodes[entry_name] = factory
helpers(entry_name)
2019-02-15 15:39:39 +05:30
end
# rubocop: enable CodeReuse/ActiveRecord
2020-04-08 14:13:33 +05:30
def dynamic_helpers(*nodes)
helpers(*nodes, dynamic: true)
end
def helpers(*nodes, dynamic: false)
2019-02-15 15:39:39 +05:30
nodes.each do |symbol|
2020-04-08 14:13:33 +05:30
if method_defined?("#{symbol}_defined?") || method_defined?("#{symbol}_entry") || method_defined?("#{symbol}_value")
raise ArgumentError, "Method '#{symbol}_defined?', '#{symbol}_entry' or '#{symbol}_value' already defined in '#{name}'"
end
unless @nodes.to_h[symbol]
raise ArgumentError, "Entry for #{symbol} is undefined" unless dynamic
end
2019-02-15 15:39:39 +05:30
define_method("#{symbol}_defined?") do
entries[symbol]&.specified?
end
2020-04-08 14:13:33 +05:30
define_method("#{symbol}_entry") do
entries[symbol]
end
2019-02-15 15:39:39 +05:30
2020-04-08 14:13:33 +05:30
define_method("#{symbol}_value") do
entry = entries[symbol]
entry.value if entry&.valid?
2019-02-15 15:39:39 +05:30
end
end
end
end
end
end
end
end