2016-06-16 23:09:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
|
|
|
module Node
|
|
|
|
##
|
|
|
|
# This mixin is responsible for adding DSL, which purpose is to
|
|
|
|
# simplifly process of adding child nodes.
|
|
|
|
#
|
|
|
|
# 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
|
2016-08-24 12:49:21 +05:30
|
|
|
include Validatable
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
included do
|
|
|
|
validations do
|
|
|
|
validates :config, type: Hash
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def compose!(deps = nil)
|
|
|
|
return unless valid?
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
self.class.nodes.each do |key, factory|
|
|
|
|
factory
|
|
|
|
.value(@config[key])
|
|
|
|
.with(key: key, parent: self)
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
@entries[key] = factory.create!
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
yield if block_given?
|
|
|
|
|
|
|
|
@entries.each_value do |entry|
|
|
|
|
entry.compose!(deps)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
2016-08-24 12:49:21 +05:30
|
|
|
def nodes
|
|
|
|
Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }]
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
private # rubocop:disable Lint/UselessAccessModifier
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def node(key, node, metadata)
|
|
|
|
factory = Node::Factory.new(node)
|
2016-06-16 23:09:34 +05:30
|
|
|
.with(description: metadata[:description])
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
(@nodes ||= {}).merge!(key.to_sym => factory)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def helpers(*nodes)
|
|
|
|
nodes.each do |symbol|
|
|
|
|
define_method("#{symbol}_defined?") do
|
2016-09-13 17:45:13 +05:30
|
|
|
@entries[symbol].specified? if @entries[symbol]
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
define_method("#{symbol}_value") do
|
2016-09-13 17:45:13 +05:30
|
|
|
return unless @entries[symbol] && @entries[symbol].valid?
|
|
|
|
|
|
|
|
@entries[symbol].value
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
alias_method symbol.to_sym, "#{symbol}_value".to_sym
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|