File: settings.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (50 lines) | stat: -rw-r--r-- 1,479 bytes parent folder | download
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
# frozen_string_literal: true

module GitlabSettings
  class Settings
    attr_reader :source

    def initialize(source, section)
      raise(ArgumentError, 'config source is required') if source.blank?
      raise(ArgumentError, 'config section is required') if section.blank?

      # Rails will set the default encoding to UTF-8
      # (https://github.com/rails/rails/blob/v6.1.7.2/railties/lib/rails.rb#L21C1-L24),
      # but it's possible this class is used before `require 'rails'` is
      # called, as in the case of `sidekiq-cluster`. Ensure the
      # configuration file is parsed as UTF-8, or
      # ActiveSupport::ConfigurationFile.parse will blow up if the
      # configuration file contains UTF-8 characters.
      Encoding.default_external = Encoding::UTF_8
      Encoding.default_internal = Encoding::UTF_8

      @source = source
      @section = section
      @loaded = false
    end

    def reload!
      yaml = ActiveSupport::ConfigurationFile.parse(source)
      all_configs = yaml.deep_stringify_keys
      configs = all_configs[section]

      @config = Options.build(configs).tap do
        @loaded = true
      end
    end

    def method_missing(name, *args)
      reload! unless @loaded

      config.public_send(name, *args) # rubocop: disable GitlabSecurity/PublicSend
    end

    def respond_to_missing?(name, include_all = false)
      config.respond_to?(name, include_all)
    end

    private

    attr_reader :config, :section
  end
end