BigW Consortium Gitlab

factory.rb 1.7 KB
Newer Older
1 2 3
module Gitlab
  module Ci
    class Config
4
      module Entry
5
        ##
6
        # Factory class responsible for fabricating entry objects.
7 8 9 10
        #
        class Factory
          class InvalidFactory < StandardError; end

11 12
          def initialize(entry)
            @entry = entry
13
            @metadata = {}
14 15 16
            @attributes = {}
          end

17 18 19 20 21
          def value(value)
            @value = value
            self
          end

22 23
          def metadata(metadata)
            @metadata.merge!(metadata)
24 25 26
            self
          end

27 28
          def with(attributes)
            @attributes.merge!(attributes)
29 30 31 32
            self
          end

          def create!
33
            raise InvalidFactory unless defined?(@value)
34

35
            ##
36
            # We assume that unspecified entry is undefined.
37 38
            # See issue #18775.
            #
39
            if @value.nil?
40
              Entry::Unspecified.new(
41
                fabricate_unspecified
42
              )
43
            else
44
              fabricate(@entry, @value)
45 46
            end
          end
47 48 49

          private

50
          def fabricate_unspecified
51
            ##
52
            # If entry has a default value we fabricate concrete node
53 54
            # with default value.
            #
55 56
            if @entry.default.nil?
              fabricate(Entry::Undefined)
57
            else
58
              fabricate(@entry, @entry.default)
59 60 61
            end
          end

62 63 64 65 66
          def fabricate(entry, value = nil)
            entry.new(value, @metadata).tap do |node|
              node.key = @attributes[:key]
              node.parent = @attributes[:parent]
              node.description = @attributes[:description]
67 68
            end
          end
69 70 71 72 73
        end
      end
    end
  end
end