summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/entry/inherit/variables.rb
blob: aa68833bdb8760aa9c9c85d227cd6828a65ceb21 (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
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Entry
        ##
        # This class represents a variables inherit entry
        #
        class Inherit
          class Variables < ::Gitlab::Config::Entry::Simplifiable
            strategy :BooleanStrategy, if: -> (config) { [true, false].include?(config) }
            strategy :ArrayStrategy, if: -> (config) { config.is_a?(Array) }

            class BooleanStrategy < ::Gitlab::Config::Entry::Boolean
              def inherit?(_key)
                value
              end
            end

            class ArrayStrategy < ::Gitlab::Config::Entry::Node
              include ::Gitlab::Config::Entry::Validatable

              validations do
                validates :config, type: Array
                validates :config, array_of_strings: true
              end

              def inherit?(key)
                value.include?(key.to_s)
              end
            end

            class UnknownStrategy < ::Gitlab::Config::Entry::Node
              def errors
                ["#{location} should be a bool or array of strings"]
              end

              def inherit?(key)
                false
              end
            end
          end
        end
      end
    end
  end
end