2016-06-09 04:08:49 -04:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
|
|
|
module Node
|
2016-06-09 07:01:19 -04:00
|
|
|
##
|
|
|
|
# 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: ...
|
|
|
|
#
|
2016-06-09 04:08:49 -04:00
|
|
|
module Configurable
|
|
|
|
extend ActiveSupport::Concern
|
2016-06-17 03:33:32 -04:00
|
|
|
include Validatable
|
2016-06-09 04:08:49 -04:00
|
|
|
|
2016-06-16 08:16:33 -04:00
|
|
|
included do
|
|
|
|
validations do
|
2016-06-21 05:43:32 -04:00
|
|
|
validates :config, type: Hash
|
2016-06-16 08:16:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-09 04:08:49 -04:00
|
|
|
private
|
|
|
|
|
2016-07-15 16:35:29 -04:00
|
|
|
def compose!
|
|
|
|
self.class.nodes.each do |key, factory|
|
|
|
|
factory
|
|
|
|
.value(@config[key])
|
|
|
|
.with(key: key, parent: self)
|
2016-06-29 03:39:04 -04:00
|
|
|
|
2016-07-15 16:35:29 -04:00
|
|
|
@entries[key] = factory.create!
|
|
|
|
end
|
2016-06-09 04:08:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
2016-06-17 03:33:32 -04:00
|
|
|
def nodes
|
2016-06-24 04:13:55 -04:00
|
|
|
Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }]
|
2016-06-13 02:50:12 -04:00
|
|
|
end
|
2016-06-09 04:08:49 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-07-10 14:59:18 -04:00
|
|
|
def node(key, node, metadata)
|
|
|
|
factory = Node::Factory.new(node)
|
2016-06-23 07:51:07 -04:00
|
|
|
.with(description: metadata[:description])
|
|
|
|
|
2016-07-10 14:59:18 -04:00
|
|
|
(@nodes ||= {}).merge!(key.to_sym => factory)
|
2016-06-09 04:08:49 -04:00
|
|
|
end
|
2016-06-24 04:13:55 -04:00
|
|
|
|
|
|
|
def helpers(*nodes)
|
|
|
|
nodes.each do |symbol|
|
|
|
|
define_method("#{symbol}_defined?") do
|
2016-07-12 08:40:51 -04:00
|
|
|
@entries[symbol].specified? if @entries[symbol]
|
2016-06-24 04:13:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
define_method("#{symbol}_value") do
|
2016-07-14 07:14:09 -04:00
|
|
|
if @entries[symbol]
|
|
|
|
return unless @entries[symbol].valid?
|
|
|
|
@entries[symbol].value
|
|
|
|
end
|
2016-06-24 04:13:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
alias_method symbol.to_sym, "#{symbol}_value".to_sym
|
|
|
|
end
|
|
|
|
end
|
2016-06-09 04:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|