summaryrefslogtreecommitdiff
path: root/lib/gitlab/config/entry/attributable.rb
blob: c8ad25215748c2f6f16bd31b95b8b0e65f9bac41 (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
# frozen_string_literal: true

module Gitlab
  module Config
    module Entry
      module Attributable
        extend ActiveSupport::Concern

        class_methods do
          def attributes(*attributes, prefix: nil)
            attributes.flatten.each do |attribute|
              attribute_method = prefix ? "#{prefix}_#{attribute}" : attribute

              if method_defined?(attribute_method)
                raise ArgumentError, "Method '#{attribute_method}' already defined in '#{name}'"
              end

              define_method(attribute_method) do
                return unless config.is_a?(Hash)

                config[attribute]
              end

              define_method("has_#{attribute_method}?") do
                config.is_a?(Hash) && config.key?(attribute)
              end
            end
          end
        end
      end
    end
  end
end