BigW Consortium Gitlab

node.rb 1.68 KB
Newer Older
1 2 3
module Gitlab
  module Ci
    class Config
4
      module Entry
5 6 7
        ##
        # Base abstract class for each configuration entry node.
        #
8
        class Node
9
          class InvalidError < StandardError; end
10

11
          attr_reader :config, :metadata
12
          attr_accessor :key, :parent, :description
13

14
          def initialize(config, **metadata)
15
            @config = config
16
            @metadata = metadata
17
            @entries = {}
18

19
            @validator = self.class.validator.new(self)
20
            @validator.validate(:new)
21 22
          end

23
          def [](key)
24
            @entries[key] || Entry::Undefined.new
25 26
          end

27 28 29 30
          def compose!(deps = nil)
            return unless valid?

            yield if block_given?
31 32
          end

33
          def leaf?
34
            @entries.none?
35 36
          end

37
          def descendants
38
            @entries.values
39 40
          end

41 42 43 44
          def ancestors
            @parent ? @parent.ancestors + [@parent] : []
          end

45 46
          def valid?
            errors.none?
47 48
          end

49
          def errors
50
            @validator.messages + descendants.flat_map(&:errors)
51 52
          end

53
          def value
54 55 56
            if leaf?
              @config
            else
57
              meaningful = @entries.select do |_key, value|
58
                value.specified? && value.relevant?
59 60
              end

61
              Hash[meaningful.map { |key, entry| [key, entry.value] }]
62
            end
63 64
          end

65
          def specified?
66 67 68
            true
          end

69 70 71 72
          def relevant?
            true
          end

73 74 75
          def self.default
          end

76 77
          def self.validator
            Validator
78
          end
79 80 81 82 83
        end
      end
    end
  end
end