diff options
Diffstat (limited to 'lib/gitlab/fake_application_settings.rb')
-rw-r--r-- | lib/gitlab/fake_application_settings.rb | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/lib/gitlab/fake_application_settings.rb b/lib/gitlab/fake_application_settings.rb index 211c0967f89..0b9a4c161ae 100644 --- a/lib/gitlab/fake_application_settings.rb +++ b/lib/gitlab/fake_application_settings.rb @@ -1,35 +1,51 @@ # frozen_string_literal: true -# This class extends an OpenStruct object by adding predicate methods to mimic +# Fakes ActiveRecord attribute storage by adding predicate methods to mimic # 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 # column type without parsing db/structure.sql. module Gitlab - class FakeApplicationSettings < OpenStruct - include ApplicationSettingImplementation - - # Mimic ActiveRecord predicate methods for boolean values - def self.define_predicate_methods(options) - options.each do |key, value| - next if key.to_s.end_with?('?') - next unless [true, false].include?(value) - - define_method "#{key}?" do - actual_key = key.to_s.chomp('?') - self[actual_key] + 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 end end end - def initialize(options = {}) - super + def initialize(settings = {}) + @table = settings.dup - FakeApplicationSettings.define_predicate_methods(options) + FakeApplicationSettings.define_properties(settings) end - alias_method :read_attribute, :[] - alias_method :has_attribute?, :[] + 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 end end |