Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fake_application_settings.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b9a4c161aeb1bf36b643b4493a43c7e3fceefb2 (plain)
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
51
52
# frozen_string_literal: true

# 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
    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(settings = {})
      @table = settings.dup

      FakeApplicationSettings.define_properties(settings)
    end

    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

Gitlab::FakeApplicationSettings.prepend_mod_with('Gitlab::FakeApplicationSettings')