summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/node/entry.rb
blob: 0c782c422b583d706b4ab33a9764bc2969ca1ef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module Gitlab
  module Ci
    class Config
      module Node
        ##
        # Base abstract class for each configuration entry node.
        #
        class Entry
          class InvalidError < StandardError; end

          attr_reader :config, :metadata
          attr_accessor :key, :parent, :description

          def initialize(config, **metadata)
            @config = config
            @metadata = metadata
            @entries = {}

            @validator = self.class.validator.new(self)
            @validator.validate(:new)
          end

          def process!
            return unless valid?

            compose!
            descendants.each(&:process!)
          end

          def leaf?
            @entries.none?
          end

          def descendants
            @entries.values
          end

          def ancestors
            @parent ? @parent.ancestors + [@parent] : []
          end

          def valid?
            errors.none?
          end

          def errors
            @validator.messages + descendants.flat_map(&:errors)
          end

          def value
            if leaf?
              @config
            else
              meaningful = @entries.select do |_key, value|
                value.specified? && value.relevant?
              end

              Hash[meaningful.map { |key, entry| [key, entry.value] }]
            end
          end

          def specified?
            true
          end

          def relevant?
            true
          end

          def self.default
          end

          def self.validator
            Validator
          end

          private

          def compose!
          end
        end
      end
    end
  end
end