Module: Gitlab::Utils::Override

Included in:
API::Helpers::Packages::BasicAuthHelpers, Backup::Repository::InterlockSizedQueue, DesignManagement::Repository, ExtractsPath, FinderWithCrossProjectAccess, Analytics::CycleAnalytics::StageEvents::StageEvent, Asciidoc::IncludeProcessor, Auth::Atlassian::IdentityLinker, Auth::Ldap::User, Auth::Saml::IdentityLinker, Auth::Saml::User, BackgroundMigration::CopyMergeRequestTargetProjectToMergeRequestMetrics, Ci::Config::External::File::Artifact, Ci::Config::External::File::Local, Ci::Config::External::File::Project, Diff::FileCollection::MergeRequestDiffBase, Git::RuggedImpl::Blob::ClassMethods, Git::RuggedImpl::Commit, Git::RuggedImpl::Commit::ClassMethods, Git::RuggedImpl::Repository, Git::RuggedImpl::Tree::ClassMethods, GitAccessDesign, GitAccessProject, GitAccessSnippet, GitAccessWiki, Jira::HttpClient, Kubernetes::CiliumNetworkPolicy, Kubernetes::NetworkPolicy, SidekiqDaemon::Monitor, Gitlab::UserAccessSnippet, GroupClusterablePresenter, GroupsController, Import::BitbucketController, Import::BitbucketServerController, Import::FogbugzController, Import::GiteaController, Import::GithubController, Import::GitlabController, Import::ManifestController, InstanceClusterablePresenter, IssueLinks::ListService, Jira::Requests::Projects::ListService, JiraService, Ldap::OmniauthCallbacksController, Measurable, MergeRequest, MergeRequests::BaseService, MergeRequests::MergeToRefService, MergeRequests::UpdateService, Metrics::Dashboard::CustomMetricEmbedService, Metrics::Dashboard::TransientEmbedService, PersonalAccessToken, ProjectClusterablePresenter, Projects::BlobController, Projects::HashedStorage::MigrateAttachmentsService, Resolvers::BaseResolver, Snippet, Todos::Destroy::ConfidentialIssueService, Todos::Destroy::EntityLeaveService, Todos::Destroy::GroupPrivateService, Todos::Destroy::ProjectPrivateService, UserDetail, Wiki
Defined in:
lib/gitlab/utils/override.rb

Defined Under Namespace

Classes: Extension

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extensionsObject


172
173
174
# File 'lib/gitlab/utils/override.rb', line 172

def self.extensions
  @extensions ||= {}
end

.verify!Object


176
177
178
# File 'lib/gitlab/utils/override.rb', line 176

def self.verify!
  extensions.values.each(&:verify!)
end

Instance Method Details

#extended(mod = nil) ⇒ Object


153
154
155
156
157
# File 'lib/gitlab/utils/override.rb', line 153

def extended(mod = nil)
  super

  queue_verification(mod.singleton_class) if mod
end

#included(base = nil) ⇒ Object


140
141
142
143
144
# File 'lib/gitlab/utils/override.rb', line 140

def included(base = nil)
  super

  queue_verification(base) if base
end

#method_added(method_name) ⇒ Object


126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gitlab/utils/override.rb', line 126

def method_added(method_name)
  super

  return unless ENV['STATIC_VERIFICATION']
  return unless Override.extensions[self]&.verify_override?(method_name)

  method_arity = instance_method(method_name).arity
  if is_a?(Class)
    Extension.verify_class!(self, method_name, method_arity)
  else # We delay the check for modules
    Override.extensions[self].add_method_name(method_name, method_arity)
  end
end

#override(method_name) ⇒ Object

Instead of writing patterns like this:

def f
  raise NotImplementedError unless defined?(super)

  true
end

We could write it like:

extend ::Gitlab::Utils::Override

override :f
def f
  true
end

This would make sure we're overriding something. See: gitlab.com/gitlab-org/gitlab/issues/1819


119
120
121
122
123
124
# File 'lib/gitlab/utils/override.rb', line 119

def override(method_name)
  return unless ENV['STATIC_VERIFICATION']

  Override.extensions[self] ||= Extension.new(self)
  Override.extensions[self].add_method_name(method_name)
end

#prepended(base = nil) ⇒ Object


146
147
148
149
150
151
# File 'lib/gitlab/utils/override.rb', line 146

def prepended(base = nil)
  super

  # prepend can override methods, thus we need to verify it like classes
  queue_verification(base, verify: true) if base
end

#queue_verification(base, verify: false) ⇒ Object


159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gitlab/utils/override.rb', line 159

def queue_verification(base, verify: false)
  return unless ENV['STATIC_VERIFICATION']

  # We could check for Class in `override`
  # This could be `nil` if `override` was never called.
  # We also force verification for prepend because it can also override
  # a method like a class, but not the cases for include or extend.
  # This includes Rails helpers but not limited to.
  if base.is_a?(Class) || verify
    Override.extensions[self]&.add_class(base)
  end
end