environment.rb 1.25 KB
Newer Older
1 2 3 4 5
module Gitlab
  module Ci
    class Config
      module Node
        ##
6
        # Entry that represents an environment.
7 8 9 10
        #
        class Environment < Entry
          include Validatable

11 12
          ALLOWED_KEYS = %i[name url]

13
          validations do
14 15
            validates :config, allowed_keys: ALLOWED_KEYS, if: :hash?

16
            validates :name, presence: true
17

18 19 20 21 22
            validates :url,
                      length: { maximum: 255 },
                      allow_nil: true,
                      addressable_url: true

23
            validate do
24 25
              unless hash? || string?
                errors.add(:config, 'should be a hash or a string')
26 27
              end
            end
28 29 30 31 32 33 34 35 36
          end

          def hash?
            @config.is_a?(Hash)
          end

          def string?
            @config.is_a?(String)
          end
37

38
          def name
39 40 41
            case @config.type
            when String then @config
            when Hash then @config[:name]
42 43 44
            end
          end

45 46 47 48
          def url
            @config[:url] if hash?
          end

49
          def value
50 51 52
            case @config.type
            when String then { name: @config }
            when Hash then @config
53
            end
54 55 56 57 58 59
          end
        end
      end
    end
  end
end