factory.rb 922 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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

18 19
          def with(attributes)
            @attributes.merge!(attributes)
20 21 22
            self
          end

23
          def nullify!
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
            @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]
            end
          end
        end
      end
    end
  end
end