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

73 lines
1.9 KiB
Ruby
Raw Normal View History

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-08-24 12:49:21 +05:30
included do
validations do
validates :config, type: Hash
end
end
private
2016-09-13 17:45:13 +05:30
def compose!
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
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
2016-09-13 17:45:13 +05:30
def node(key, node, metadata)
factory = Node::Factory.new(node)
.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
2016-09-13 17:45:13 +05:30
@entries[symbol].specified? if @entries[symbol]
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
alias_method symbol.to_sym, "#{symbol}_value".to_sym
end
end
end
end
end
end
end
end