summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/node/entry.rb
blob: 3043dc4c61f85dc6f74aa4796aa827fd46092329 (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
module Gitlab
  module Ci
    class Config
      module Node
        class Entry
          attr_reader :value, :nodes, :parent

          def initialize(value, root = nil, parent = nil)
            @value = value
            @root = root
            @parent = parent
            @nodes, @errors = [], []

            keys.each_key do |key|
              instance_variable_set("@#{key}", Null.new(nil, root, self))
            end

            unless leaf? || value.is_a?(Hash)
              @errors << 'should be a configuration entry with hash value'
            end
          end

          def process!
            return if leaf? || !valid?

            keys.each do |key, entry_class|
              next unless @value.has_key?(key)

              entry = entry_class.new(@value[key], @root, self)
              instance_variable_set("@#{key}", entry)
              @nodes.append(entry)
            end

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

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

          def valid?
            errors.none?
          end

          def leaf?
            keys.none?
          end

          def keys
            self.class.nodes || {}
          end

          def validate!
            raise NotImplementedError
          end

          class << self
            attr_reader :nodes

            private

            def add_node(symbol, entry_class)
              (@nodes ||= {}).merge!(symbol.to_sym => entry_class)
            end
          end
        end
      end
    end
  end
end