BigW Consortium Gitlab

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

11 12
          def initialize(node)
            @node = node
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 41 42
              Node::Undefined.new(
                fabricate_undefined
              )
43
            else
44
              fabricate(@node, @value)
45 46
            end
          end
47 48 49

          private

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

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