summaryrefslogtreecommitdiff
path: root/lib/gitlab/fake_application_settings.rb
blob: 2c827265d8c777cc7fbf9f3d6b34824126b36978 (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
# This class extends an OpenStruct object 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/schema.rb.
module Gitlab
  class FakeApplicationSettings < OpenStruct
    # 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]
        end
      end
    end

    def initialize(options = {})
      super

      FakeApplicationSettings.define_predicate_methods(options)
    end

    def key_restriction_for(type)
      0
    end

    def allowed_key_types
      ApplicationSetting::SUPPORTED_KEY_TYPES
    end

    def pick_repository_storage
      repository_storages.sample
    end
  end
end