Class: GitlabSettings::Options

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gitlab_settings/options.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Options

Returns a new instance of Options.



52
53
54
# File 'lib/gitlab_settings/options.rb', line 52

def initialize(value)
  @options = value.deep_stringify_keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/gitlab_settings/options.rb', line 142

def method_missing(name, *args, &block)
  name_string = +name.to_s

  if name_string.chomp!("=")
    return self[name_string] = args.first if key?(name_string)
  elsif key?(name_string)
    return self[name_string]
  end

  if @options.respond_to?(name)
    error_msg = "Calling a hash method on #{self.class}: `#{name}`"

    log_and_raise_dev_exception(error_msg, method: name)

    return @options.public_send(name, *args, &block) # rubocop: disable GitlabSecurity/PublicSend
  end

  raise ::GitlabSettings::MissingSetting, "option '#{name}' not defined"
end

Class Method Details

.build(obj) ⇒ Object

Recursively build GitlabSettings::Options



41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab_settings/options.rb', line 41

def self.build(obj)
  case obj
  when Hash
    new(obj.transform_values { |value| build(value) })
  when Array
    obj.map { |value| build(value) }
  else
    obj
  end
end

Instance Method Details

#[](key) ⇒ Object



56
57
58
# File 'lib/gitlab_settings/options.rb', line 56

def [](key)
  @options[key.to_s]
end

#[]=(key, value) ⇒ Object



60
61
62
# File 'lib/gitlab_settings/options.rb', line 60

def []=(key, value)
  @options[key.to_s] = self.class.build(value)
end

#deep_merge(other) ⇒ Object



96
97
98
# File 'lib/gitlab_settings/options.rb', line 96

def deep_merge(other)
  self.class.build(to_hash.deep_merge(other.deep_stringify_keys))
end

#deep_merge!(other) ⇒ Object



100
101
102
# File 'lib/gitlab_settings/options.rb', line 100

def deep_merge!(other)
  @options = to_hash.deep_merge(other.deep_stringify_keys)
end

#defaultObject

Some configurations use the ‘default’ key, like: gitlab.com/gitlab-org/gitlab/-/blob/c4d5c77c87494bb320fa7fdf19b0e4d7d52af1d1/spec/support/helpers/stub_configuration.rb#L96 But since ‘default` is also a method in Hash, this can be confusing and raise an exception instead of returning nil, as expected in some places. To avoid that, we use #default always as a possible internal key



74
75
76
# File 'lib/gitlab_settings/options.rb', line 74

def default
  @options['default']
end

#dupObject



84
85
86
# File 'lib/gitlab_settings/options.rb', line 84

def dup
  self.class.build(to_hash)
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/gitlab_settings/options.rb', line 104

def is_a?(klass)
  return true if klass == Hash

  super(klass)
end

#key?(key) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


64
65
66
# File 'lib/gitlab_settings/options.rb', line 64

def key?(key)
  @options.key?(key.to_s)
end

#merge(other) ⇒ Object



88
89
90
# File 'lib/gitlab_settings/options.rb', line 88

def merge(other)
  self.class.build(to_hash.merge(other.deep_stringify_keys))
end

#merge!(other) ⇒ Object



92
93
94
# File 'lib/gitlab_settings/options.rb', line 92

def merge!(other)
  @options = to_hash.merge(other.deep_stringify_keys)
end

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

Returns:

  • (Boolean)


162
163
164
165
166
# File 'lib/gitlab_settings/options.rb', line 162

def respond_to_missing?(name, include_all = false)
  return true if key?(name)

  @options.respond_to?(name, include_all)
end

#stringify_keys!Object Also known as: deep_stringify_keys!

Don’t alter the internal keys



123
124
125
126
127
128
129
# File 'lib/gitlab_settings/options.rb', line 123

def stringify_keys!
  error_msg = "Warning: Do not mutate #{self.class} objects: `#{__method__}`"

  log_and_raise_dev_exception(error_msg, method: __method__)

  to_hash.deep_stringify_keys
end

#symbolize_keys!Object Also known as: deep_symbolize_keys!

Don’t alter the internal keys



133
134
135
136
137
138
139
# File 'lib/gitlab_settings/options.rb', line 133

def symbolize_keys!
  error_msg = "Warning: Do not mutate #{self.class} objects: `#{__method__}`"

  log_and_raise_dev_exception(error_msg, method: __method__)

  to_hash.deep_symbolize_keys
end

#to_hashObject Also known as: to_h



110
111
112
113
114
115
116
117
118
119
# File 'lib/gitlab_settings/options.rb', line 110

def to_hash
  @options.deep_transform_values do |option|
    case option
    when self.class
      option.to_hash
    else
      option
    end
  end
end

#with_indifferent_accessObject



80
81
82
# File 'lib/gitlab_settings/options.rb', line 80

def with_indifferent_access
  to_hash.with_indifferent_access
end