Class: GitlabSettings::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_settings/settings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, section) ⇒ Settings

Returns a new instance of Settings.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab_settings/settings.rb', line 7

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



36
37
38
39
40
# File 'lib/gitlab_settings/settings.rb', line 36

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

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

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/gitlab_settings/settings.rb', line 5

def source
  @source
end

Instance Method Details

#reload!Object



26
27
28
29
30
31
32
33
34
# File 'lib/gitlab_settings/settings.rb', line 26

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

#respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/gitlab_settings/settings.rb', line 42

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