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

77 lines
2 KiB
Ruby
Raw Normal View History

module Gitlab
module Ci
class Config
2017-08-17 22:00:37 +05:30
module Entry
##
# 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-08-24 12:49:21 +05:30
included do
validations do
validates :config, type: Hash
end
end
2016-09-29 09:46:39 +05:30
def compose!(deps = nil)
return unless valid?
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
end
class_methods do
2016-08-24 12:49:21 +05:30
def nodes
Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }]
end
2016-09-13 17:45:13 +05:30
private # rubocop:disable Lint/UselessAccessModifier
2017-08-17 22:00:37 +05:30
def entry(key, entry, metadata)
factory = Entry::Factory.new(entry)
.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-08-24 12:49:21 +05:30
def helpers(*nodes)
nodes.each do |symbol|
define_method("#{symbol}_defined?") do
2017-08-17 22:00:37 +05:30
@entries[symbol]&.specified?
2016-08-24 12:49:21 +05:30
end
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
end
end
end
end
end
end
end
end