2016-06-16 23:09:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
|
|
|
module Node
|
|
|
|
##
|
|
|
|
# Base abstract class for each configuration entry node.
|
|
|
|
#
|
|
|
|
class Entry
|
|
|
|
class InvalidError < StandardError; end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
attr_reader :config
|
|
|
|
attr_accessor :key, :parent, :description
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def initialize(config)
|
|
|
|
@config = config
|
2016-06-16 23:09:34 +05:30
|
|
|
@nodes = {}
|
2016-08-24 12:49:21 +05:30
|
|
|
@validator = self.class.validator.new(self)
|
|
|
|
@validator.validate
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def process!
|
|
|
|
return if leaf?
|
|
|
|
return unless valid?
|
|
|
|
|
|
|
|
compose!
|
2016-08-24 12:49:21 +05:30
|
|
|
process_nodes!
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def nodes
|
|
|
|
@nodes.values
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def leaf?
|
|
|
|
self.class.nodes.none?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def ancestors
|
|
|
|
@parent ? @parent.ancestors + [@parent] : []
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
errors.none?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def errors
|
2016-08-24 12:49:21 +05:30
|
|
|
@validator.messages + nodes.flat_map(&:errors)
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def value
|
|
|
|
if leaf?
|
|
|
|
@config
|
|
|
|
else
|
|
|
|
defined = @nodes.select { |_key, value| value.defined? }
|
|
|
|
Hash[defined.map { |key, node| [key, node.value] }]
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def defined?
|
|
|
|
true
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def self.default
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def self.nodes
|
|
|
|
{}
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def self.validator
|
|
|
|
Validator
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
private
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def compose!
|
2016-08-24 12:49:21 +05:30
|
|
|
self.class.nodes.each do |key, essence|
|
2016-06-16 23:09:34 +05:30
|
|
|
@nodes[key] = create_node(key, essence)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def process_nodes!
|
|
|
|
nodes.each(&:process!)
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def create_node(key, essence)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|