BigW Consortium Gitlab

node.rb 1.9 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
          InvalidError = Class.new(StandardError)
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
            true
67 68
          end

69 70 71 72
          def relevant?
            true
          end

73
          def inspect
74 75
            val = leaf? ? config : descendants
            unspecified = specified? ? '' : '(unspecified) '
76 77 78
            "#<#{self.class.name} #{unspecified}{#{key}: #{val.inspect}}>"
          end

79 80 81
          def self.default
          end

82 83
          def self.validator
            Validator
84
          end
85 86 87 88 89
        end
      end
    end
  end
end