summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/node/factory.rb
blob: 025ae40ef944ebb5a8ea4ccbe5d8a7949f11142a (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
module Gitlab
  module Ci
    class Config
      module Node
        ##
        # Factory class responsible for fabricating node entry objects.
        #
        # It uses Fluent Interface pattern to set all necessary attributes.
        #
        class Factory
          class InvalidFactory < StandardError; end

          def initialize(entry_class)
            @entry_class = entry_class
            @attributes = {}
          end

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

          def nullify!
            @entry_class = Node::Null
            self
          end

          def create!
            raise InvalidFactory unless @attributes.has_key?(:value)

            @entry_class.new(@attributes[:value]).tap do |entry|
              entry.description = @attributes[:description]
              entry.key = @attributes[:key]
            end
          end
        end
      end
    end
  end
end