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

104 lines
2.2 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
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 = {}
2018-03-17 18:26:18 +05:30
self.class.aspects.to_a.each do |aspect|
instance_exec(&aspect)
end
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
2018-03-17 18:26:18 +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
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
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
2018-03-17 18:26:18 +05:30
def self.aspects
@aspects ||= []
end
2018-03-17 18:26:18 +05:30
private
attr_reader :entries
end
end
end
end
end