summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/node/entry.rb
blob: f044ef965e9c1a19b100a2f9bd9d55caf96a724a (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
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
          attr_accessor :key, :description

          def initialize(config)
            @config = config
            @nodes = {}
            @validator = self.class.validator.new(self)
            @validator.validate
          end

          def process!
            return if leaf?
            return unless valid?

            compose!
            process_nodes!
          end

          def nodes
            @nodes.values
          end

          def leaf?
            self.class.nodes.none?
          end

          def key
            @key || self.class.name.demodulize.underscore
          end

          def valid?
            errors.none?
          end

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

          def value
            raise NotImplementedError
          end

          def self.nodes
            {}
          end

          def self.validator
            Validator
          end

          private

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

          def process_nodes!
            nodes.each(&:process!)
          end

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