2016-06-16 23:09:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
2017-08-17 22:00:37 +05:30
|
|
|
module Entry
|
2016-06-16 23:09:34 +05:30
|
|
|
##
|
|
|
|
# Base abstract class for each configuration entry node.
|
|
|
|
#
|
2017-08-17 22:00:37 +05:30
|
|
|
class Node
|
|
|
|
InvalidError = Class.new(StandardError)
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
attr_reader :config, :metadata
|
2016-08-24 12:49:21 +05:30
|
|
|
attr_accessor :key, :parent, :description
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def initialize(config, **metadata)
|
2016-08-24 12:49:21 +05:30
|
|
|
@config = config
|
2016-09-13 17:45:13 +05:30
|
|
|
@metadata = metadata
|
|
|
|
@entries = {}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
self.class.aspects.to_a.each do |aspect|
|
|
|
|
instance_exec(&aspect)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def [](key)
|
2017-08-17 22:00:37 +05:30
|
|
|
@entries[key] || Entry::Undefined.new
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def compose!(deps = nil)
|
2016-06-16 23:09:34 +05:30
|
|
|
return unless valid?
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
yield if block_given?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def leaf?
|
|
|
|
@entries.none?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def descendants
|
|
|
|
@entries.values
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
[]
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def value
|
|
|
|
if leaf?
|
|
|
|
@config
|
|
|
|
else
|
2016-09-13 17:45:13 +05:30
|
|
|
meaningful = @entries.select do |_key, value|
|
|
|
|
value.specified? && value.relevant?
|
|
|
|
end
|
|
|
|
|
|
|
|
Hash[meaningful.map { |key, entry| [key, entry.value] }]
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def specified?
|
2016-08-24 12:49:21 +05:30
|
|
|
true
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def relevant?
|
|
|
|
true
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def location
|
|
|
|
name = @key.presence || self.class.name.to_s.demodulize
|
|
|
|
.underscore.humanize.downcase
|
|
|
|
|
|
|
|
ancestors.map(&:key).append(name).compact.join(':')
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def inspect
|
|
|
|
val = leaf? ? config : descendants
|
|
|
|
unspecified = specified? ? '' : '(unspecified) '
|
|
|
|
"#<#{self.class.name} #{unspecified}{#{key}: #{val.inspect}}>"
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def self.default
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def self.aspects
|
|
|
|
@aspects ||= []
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :entries
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|