2021-01-29 00:20:46 +05:30
---
stage: none
group: unassigned
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2021-01-29 00:20:46 +05:30
---
2018-03-17 18:26:18 +05:30
# GitLab utilities
2019-12-26 22:10:19 +05:30
We have developed a number of utilities to help ease development:
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
## `MergeHash`
2021-09-04 01:27:46 +05:30
Refer to [`merge_hash.rb` ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/utils/merge_hash.rb ):
2018-03-17 18:26:18 +05:30
2019-03-02 22:35:43 +05:30
- Deep merges an array of hashes:
2018-03-17 18:26:18 +05:30
2021-09-04 01:27:46 +05:30
```ruby
2019-10-12 21:52:04 +05:30
Gitlab::Utils::MergeHash.merge(
[{ hello: ["world"] },
{ hello: "Everyone" },
{ hello: { greetings: ['Bonjour', 'Hello', 'Hallo', 'Dzien dobry'] } },
"Goodbye", "Hallo"]
)
```
Gives:
2021-09-04 01:27:46 +05:30
```ruby
2019-10-12 21:52:04 +05:30
[
{
hello:
[
"world",
"Everyone",
{ greetings: ['Bonjour', 'Hello', 'Hallo', 'Dzien dobry'] }
]
},
"Goodbye"
]
```
2018-03-17 18:26:18 +05:30
2019-03-02 22:35:43 +05:30
- Extracts all keys and values from a hash into an array:
2018-03-17 18:26:18 +05:30
2021-09-04 01:27:46 +05:30
```ruby
2019-10-12 21:52:04 +05:30
Gitlab::Utils::MergeHash.crush(
{ hello: "world", this: { crushes: ["an entire", "hash"] } }
)
```
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
Gives:
2018-03-17 18:26:18 +05:30
2021-09-04 01:27:46 +05:30
```ruby
2019-10-12 21:52:04 +05:30
[:hello, "world", :this, :crushes, "an entire", "hash"]
```
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
## `Override`
2021-09-04 01:27:46 +05:30
Refer to [`override.rb` ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/utils/override.rb ):
2018-03-17 18:26:18 +05:30
2019-12-26 22:10:19 +05:30
- This utility can help you check if one method would override
another or not. It is the same concept as Java's `@Override` annotation
2020-01-01 13:55:28 +05:30
or Scala's `override` keyword. However, we only run this check when
2018-03-17 18:26:18 +05:30
`ENV['STATIC_VERIFICATION']` is set to avoid production runtime overhead.
2019-12-26 22:10:19 +05:30
This is useful for checking:
2018-03-17 18:26:18 +05:30
2019-12-26 22:10:19 +05:30
- If you have typos in overriding methods.
- If you renamed the overridden methods, which make the original override methods
irrelevant.
2018-03-17 18:26:18 +05:30
Here's a simple example:
2021-09-04 01:27:46 +05:30
```ruby
2018-03-17 18:26:18 +05:30
class Base
def execute
end
end
class Derived < Base
extend ::Gitlab::Utils::Override
override :execute # Override check happens here
def execute
end
end
```
This also works on modules:
2021-09-04 01:27:46 +05:30
```ruby
2018-03-17 18:26:18 +05:30
module Extension
extend ::Gitlab::Utils::Override
override :execute # Modules do not check this immediately
def execute
end
end
class Derived < Base
prepend Extension # Override check happens here, not in the module
end
```
2021-02-22 17:27:13 +05:30
Note that the check only happens when either:
2020-01-01 13:55:28 +05:30
- The overriding method is defined in a class, or:
- The overriding method is defined in a module, and it's prepended to
a class or a module.
Because only a class or prepended module can actually override a method.
Including or extending a module into another cannot override anything.
2021-03-11 19:13:27 +05:30
### Interactions with `ActiveSupport::Concern`, `prepend`, and `class_methods`
When you use `ActiveSupport::Concern` that includes class methods, you do not
get expected results because `ActiveSupport::Concern` doesn't work like a
regular Ruby module.
Since we already have `Prependable` as a patch for `ActiveSupport::Concern`
to enable `prepend` , it has consequences with how it would interact with
`override` and `class_methods` . As a workaround, `extend` `ClassMethods`
into the defining `Prependable` module.
This allows us to use `override` to verify `class_methods` used in the
context mentioned above. This workaround only applies when we run the
verification, not when running the application itself.
Here are example code blocks that demonstrate the effect of this workaround:
following codes:
```ruby
module Base
extend ActiveSupport::Concern
class_methods do
def f
end
end
end
module Derived
include Base
end
# Without the workaround
Base.f # => NoMethodError
Derived.f # => nil
# With the workaround
Base.f # => nil
Derived.f # => nil
```
2019-12-21 20:55:43 +05:30
## `StrongMemoize`
2021-09-04 01:27:46 +05:30
Refer to [`strong_memoize.rb` ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/utils/strong_memoize.rb ):
2018-03-17 18:26:18 +05:30
2019-03-02 22:35:43 +05:30
- Memoize the value even if it is `nil` or `false` .
2018-03-17 18:26:18 +05:30
2019-12-26 22:10:19 +05:30
We often do `@value ||= compute` . However, this doesn't work well if
`compute` might eventually give `nil` and you don't want to compute again.
Instead you could use `defined?` to check if the value is set or not.
It's tedious to write such pattern, and `StrongMemoize` would
help you use such pattern.
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
Instead of writing patterns like this:
2018-03-17 18:26:18 +05:30
2021-09-04 01:27:46 +05:30
```ruby
2019-10-12 21:52:04 +05:30
class Find
def result
return @result if defined?(@result)
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
@result = search
2018-03-17 18:26:18 +05:30
end
2019-10-12 21:52:04 +05:30
end
```
2018-03-17 18:26:18 +05:30
2019-12-26 22:10:19 +05:30
You could write it like:
2018-03-17 18:26:18 +05:30
2022-08-27 11:52:29 +05:30
```ruby
class Find
include Gitlab::Utils::StrongMemoize
def result
search
end
strong_memoize_attr :result
def enabled?
Feature.enabled?(:some_feature)
end
2023-03-17 16:20:25 +05:30
strong_memoize_attr :enabled?
2023-01-13 00:05:48 +05:30
end
```
2023-03-04 22:38:38 +05:30
Using `strong_memoize_attr` on methods with parameters is not supported.
It does not work when combined with [`override` ](#override ) and might memoize wrong results.
Use `strong_memoize_with` instead.
```ruby
# bad
def expensive_method(arg)
# ...
end
strong_memoize_attr :expensive_method
# good
def expensive_method(arg)
strong_memoize_with(:expensive_method, arg)
# ...
end
end
```
2023-01-13 00:05:48 +05:30
There's also `strong_memoize_with` to help memoize methods that take arguments.
This should be used for methods that have a low number of possible values
as arguments or with consistent repeating arguments in a loop.
```ruby
class Find
include Gitlab::Utils::StrongMemoize
def result(basic: true)
strong_memoize_with(:result, basic) do
search(basic)
end
end
2022-08-27 11:52:29 +05:30
end
```
2019-03-02 22:35:43 +05:30
- Clear memoization
2018-03-17 18:26:18 +05:30
2021-09-04 01:27:46 +05:30
```ruby
2019-10-12 21:52:04 +05:30
class Find
include Gitlab::Utils::StrongMemoize
end
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
Find.new.clear_memoization(:result)
```
2018-11-08 19:23:39 +05:30
2019-12-21 20:55:43 +05:30
## `RequestCache`
2021-09-04 01:27:46 +05:30
Refer to [`request_cache.rb` ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/cache/request_cache.rb ).
2018-11-08 19:23:39 +05:30
This module provides a simple way to cache values in RequestStore,
and the cache key would be based on the class name, method name,
optionally customized instance level values, optionally customized
method level values, and optional method arguments.
2019-12-26 22:10:19 +05:30
A simple example that only uses the instance level customised values is:
2018-11-08 19:23:39 +05:30
2021-09-04 01:27:46 +05:30
```ruby
2018-11-08 19:23:39 +05:30
class UserAccess
extend Gitlab::Cache::RequestCache
request_cache_key do
[user& .id, project& .id]
end
request_cache def can_push_to_branch?(ref)
# ...
end
end
```
This way, the result of `can_push_to_branch?` would be cached in
`RequestStore.store` based on the cache key. If `RequestStore` is not
2019-12-26 22:10:19 +05:30
currently active, then it would be stored in a hash, and saved in an
instance variable so the cache logic would be the same.
2018-11-08 19:23:39 +05:30
We can also set different strategies for different methods:
2021-09-04 01:27:46 +05:30
```ruby
2018-11-08 19:23:39 +05:30
class Commit
extend Gitlab::Cache::RequestCache
def author
2018-12-13 13:39:08 +05:30
User.find_by_any_email(author_email)
2018-11-08 19:23:39 +05:30
end
2018-12-13 13:39:08 +05:30
request_cache(:author) { author_email }
2018-11-08 19:23:39 +05:30
end
```
2019-12-26 22:10:19 +05:30
## `ReactiveCaching`
2020-03-13 15:44:24 +05:30
Read the documentation on [`ReactiveCaching` ](reactive_caching.md ).