Welcome to mirror list, hosted at ThFree Co, Russian Federation.

factory.rb « entry « config « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 85c9c3511a4c8b6b68377e4e7f4e6bdfe7f45047 (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
72
73
74
75
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Entry
        ##
        # Factory class responsible for fabricating entry objects.
        #
        class Factory
          InvalidFactory = Class.new(StandardError)

          def initialize(entry)
            @entry = entry
            @metadata = {}
            @attributes = {}
          end

          def value(value)
            @value = value
            self
          end

          def metadata(metadata)
            @metadata.merge!(metadata)
            self
          end

          def with(attributes)
            @attributes.merge!(attributes)
            self
          end

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

            ##
            # We assume that unspecified entry is undefined.
            # See issue #18775.
            #
            if @value.nil?
              Entry::Unspecified.new(
                fabricate_unspecified
              )
            else
              fabricate(@entry, @value)
            end
          end

          private

          def fabricate_unspecified
            ##
            # If entry has a default value we fabricate concrete node
            # with default value.
            #
            if @entry.default.nil?
              fabricate(Entry::Undefined)
            else
              fabricate(@entry, @entry.default)
            end
          end

          def fabricate(entry, value = nil)
            entry.new(value, @metadata).tap do |node|
              node.key = @attributes[:key]
              node.parent = @attributes[:parent]
              node.description = @attributes[:description]
            end
          end
        end
      end
    end
  end
end