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

84 lines
1.7 KiB
Ruby
Raw Normal View History

module Gitlab
module Ci
class Config
module Node
##
# Base abstract class for each configuration entry node.
#
class Entry
class InvalidError < StandardError; end
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)
@entries[key] || Node::Undefined.new
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
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