BigW Consortium Gitlab

configurable.rb 1.72 KB
Newer Older
1 2 3 4
module Gitlab
  module Ci
    class Config
      module Node
5 6 7 8 9 10 11 12 13 14 15
        ##
        # This mixin is responsible for adding DSL, which purpose is to
        # simplifly process of adding child nodes.
        #
        # This can be used only if parent node is a configuration entry that
        # holds a hash as a configuration value, for example:
        #
        # job:
        #   script: ...
        #   artifacts: ...
        #
16 17
        module Configurable
          extend ActiveSupport::Concern
18
          include Validatable
19

20 21
          included do
            validations do
22
              validates :config, type: Hash
23 24 25
            end
          end

26 27
          private

28
          def create_node(key, factory)
29
            factory.with(value: @config[key], key: key, parent: self)
30

31
            factory.create!
32 33 34
          end

          class_methods do
35
            def nodes
36
              Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }]
37
            end
38 39 40

            private

41
            def node(symbol, entry_class, metadata)
42 43 44
              factory = Node::Factory.new(entry_class)
                .with(description: metadata[:description])

45
              (@nodes ||= {}).merge!(symbol.to_sym => factory)
46
            end
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

            def helpers(*nodes)
              nodes.each do |symbol|
                define_method("#{symbol}_defined?") do
                  @nodes[symbol].try(:defined?)
                end

                define_method("#{symbol}_value") do
                  raise Entry::InvalidError unless valid?
                  @nodes[symbol].try(:value)
                end

                alias_method symbol.to_sym, "#{symbol}_value".to_sym
              end
            end
62 63 64 65 66 67
          end
        end
      end
    end
  end
end