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

90 lines
1.9 KiB
Ruby
Raw Normal View History

module Gitlab
module Ci
class Config
2017-08-17 22:00:37 +05:30
module Entry
##
# Base abstract class for each configuration entry node.
#
2017-08-17 22:00:37 +05:30
class Node
InvalidError = Class.new(StandardError)
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-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 = {}
2016-08-24 12:49:21 +05:30
@validator = self.class.validator.new(self)
2016-09-13 17:45:13 +05:30
@validator.validate(:new)
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)
return unless valid?
2016-09-29 09:46:39 +05:30
yield if block_given?
end
2016-09-13 17:45:13 +05:30
def leaf?
@entries.none?
end
2016-09-13 17:45:13 +05:30
def descendants
@entries.values
end
2016-08-24 12:49:21 +05:30
def ancestors
@parent ? @parent.ancestors + [@parent] : []
end
def valid?
errors.none?
end
def errors
2016-09-13 17:45:13 +05:30
@validator.messages + descendants.flat_map(&:errors)
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
end
2016-09-13 17:45:13 +05:30
def specified?
2016-08-24 12:49:21 +05:30
true
end
2016-09-13 17:45:13 +05:30
def relevant?
true
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-08-24 12:49:21 +05:30
def self.validator
Validator
end
end
end
end
end
end