debian-mirror-gitlab/lib/gitlab/fake_application_settings.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.3 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2021-10-27 15:23:28 +05:30
# Fakes ActiveRecord attribute storage by adding predicate methods to mimic
2017-09-10 17:25:29 +05:30
# ActiveRecord access. We rely on the initial values being true or false to
# determine whether to define a predicate method because for a newly-added
# column that has not been migrated yet, there is no way to determine the
2020-04-22 19:07:51 +05:30
# column type without parsing db/structure.sql.
2017-09-10 17:25:29 +05:30
module Gitlab
2021-10-27 15:23:28 +05:30
class FakeApplicationSettings
prepend ApplicationSettingImplementation
def self.define_properties(settings)
settings.each do |key, value|
define_method key do
read_attribute(key)
end
if [true, false].include?(value)
define_method "#{key}?" do
read_attribute(key)
end
end
define_method "#{key}=" do |v|
@table[key.to_sym] = v
2017-09-10 17:25:29 +05:30
end
end
end
2018-11-20 20:47:30 +05:30
2021-10-27 15:23:28 +05:30
def initialize(settings = {})
@table = settings.dup
2018-11-20 20:47:30 +05:30
2021-10-27 15:23:28 +05:30
FakeApplicationSettings.define_properties(settings)
2018-11-20 20:47:30 +05:30
end
2021-10-27 15:23:28 +05:30
def read_attribute(key)
@table[key.to_sym]
end
def has_attribute?(key)
@table.key?(key.to_sym)
end
# Mimic behavior of OpenStruct, which absorbs any calls into undefined
# properties to return `nil`.
def method_missing(*)
nil
end
2017-09-10 17:25:29 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Gitlab::FakeApplicationSettings.prepend_mod_with('Gitlab::FakeApplicationSettings')