summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/node/entry.rb
blob: 52758a962f3c79989cd36582be5dc86114049853 (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
module Gitlab
  module Ci
    class Config
      module Node
        ##
        # Base abstract class for each configuration entry node.
        #
        class Entry
          class InvalidError < StandardError; end

          attr_accessor :description

          def initialize(value)
            @value = value
            @nodes = {}
            @errors = []

            prevalidate!
          end

          def process!
            return if leaf?
            return unless valid?

            compose!

            nodes.each(&:process!)
            nodes.each(&:validate!)
          end

          def nodes
            @nodes.values
          end

          def valid?
            errors.none?
          end

          def leaf?
            allowed_nodes.none?
          end

          def errors
            @errors + nodes.map(&:errors).flatten
          end

          def allowed_nodes
            {}
          end

          def validate!
            raise NotImplementedError
          end

          def value
            raise NotImplementedError
          end

          private

          def prevalidate!
          end

          def compose!
            allowed_nodes.each do |key, essence|
              @nodes[key] = create_node(key, essence)
            end
          end

          def create_node(key, essence)
            raise NotImplementedError
          end
        end
      end
    end
  end
end