summaryrefslogtreecommitdiff
path: root/lib/gitlab/safe_request_store.rb
diff options
context:
space:
mode:
authorMichael Kozono <mkozono@gmail.com>2018-09-20 11:49:58 -0700
committerMichael Kozono <mkozono@gmail.com>2018-09-24 12:11:26 -0700
commit45cf64c827270d66a88d483bb3f9043a90301255 (patch)
treeb71d4455decb58e5d429b8e5299896a70fc7c20a /lib/gitlab/safe_request_store.rb
parent00bb83f7fc6d52583d56fb0f0ea4c9d951535b52 (diff)
downloadgitlab-ce-45cf64c827270d66a88d483bb3f9043a90301255.tar.gz
Use a null object with RequestStore
Makes it easier and safer to use RequestStore because you don't need to check `RequestStore.active?` before using it. You just have to use `Gitlab::SafeRequestStore` instead.
Diffstat (limited to 'lib/gitlab/safe_request_store.rb')
-rw-r--r--lib/gitlab/safe_request_store.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/gitlab/safe_request_store.rb b/lib/gitlab/safe_request_store.rb
new file mode 100644
index 00000000000..4e82353adb6
--- /dev/null
+++ b/lib/gitlab/safe_request_store.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module SafeRequestStore
+ NULL_STORE = Gitlab::NullRequestStore.new
+
+ class << self
+ # These methods should always run directly against RequestStore
+ delegate :clear!, :begin!, :end!, :active?, to: :RequestStore
+
+ # These methods will run against NullRequestStore if RequestStore is disabled
+ delegate :read, :[], :write, :[]=, :exist?, :fetch, :delete, to: :store
+ end
+
+ def self.store
+ if RequestStore.active?
+ RequestStore
+ else
+ NULL_STORE
+ end
+ end
+ end
+end