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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-18 00:08:29 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-18 00:08:29 +0300
commit40254b9ace2a74a3c9f1cc51a1b1d5e3e78c1ae9 (patch)
tree9b735ef933178be36d35088f3acab2d9b75dbbad /lib/gitlab/application_context.rb
parent22a0d312ae82e7dda3073d5d1a5a766d7641738d (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/application_context.rb')
-rw-r--r--lib/gitlab/application_context.rb43
1 files changed, 35 insertions, 8 deletions
diff --git a/lib/gitlab/application_context.rb b/lib/gitlab/application_context.rb
index b9190b519a0..71dbfea70e8 100644
--- a/lib/gitlab/application_context.rb
+++ b/lib/gitlab/application_context.rb
@@ -5,6 +5,14 @@ module Gitlab
class ApplicationContext
include Gitlab::Utils::LazyAttributes
+ Attribute = Struct.new(:name, :type)
+
+ APPLICATION_ATTRIBUTES = [
+ Attribute.new(:project, Project),
+ Attribute.new(:namespace, Namespace),
+ Attribute.new(:user, User)
+ ].freeze
+
def self.with_context(args, &block)
application_context = new(**args)
Labkit::Context.with_context(application_context.to_lazy_hash, &block)
@@ -15,21 +23,36 @@ module Gitlab
Labkit::Context.push(application_context.to_lazy_hash)
end
- def initialize(user: nil, project: nil, namespace: nil)
- @user, @project, @namespace = user, project, namespace
+ def initialize(**args)
+ unknown_attributes = args.keys - APPLICATION_ATTRIBUTES.map(&:name)
+ raise ArgumentError, "#{unknown_attributes} are not known keys" if unknown_attributes.any?
+
+ @set_values = args.keys
+
+ assign_attributes(args)
end
def to_lazy_hash
- { user: -> { username },
- project: -> { project_path },
- root_namespace: -> { root_namespace_path } }
+ {}.tap do |hash|
+ hash[:user] = -> { username } if set_values.include?(:user)
+ hash[:project] = -> { project_path } if set_values.include?(:project)
+ hash[:root_namespace] = -> { root_namespace_path } if include_namespace?
+ end
end
private
- lazy_attr_reader :user, type: User
- lazy_attr_reader :project, type: Project
- lazy_attr_reader :namespace, type: Namespace
+ attr_reader :set_values
+
+ APPLICATION_ATTRIBUTES.each do |attr|
+ lazy_attr_reader attr.name, type: attr.type
+ end
+
+ def assign_attributes(values)
+ values.slice(*APPLICATION_ATTRIBUTES.map(&:name)).each do |name, value|
+ instance_variable_set("@#{name}", value)
+ end
+ end
def project_path
project&.full_path
@@ -46,5 +69,9 @@ module Gitlab
project&.full_path_components&.first
end
end
+
+ def include_namespace?
+ set_values.include?(:namespace) || set_values.include?(:project)
+ end
end
end