debian-mirror-gitlab/doc/development/utilities.md

179 lines
4 KiB
Markdown
Raw Normal View History

2018-03-17 18:26:18 +05:30
# GitLab utilities
We developed a number of utilities to ease development.
2019-12-04 20:38:33 +05:30
## [`MergeHash`](https://gitlab.com/gitlab-org/gitlab-foss/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
2019-10-12 21:52:04 +05:30
``` ruby
Gitlab::Utils::MergeHash.merge(
[{ hello: ["world"] },
{ hello: "Everyone" },
{ hello: { greetings: ['Bonjour', 'Hello', 'Hallo', 'Dzien dobry'] } },
"Goodbye", "Hallo"]
)
```
Gives:
``` ruby
[
{
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
2019-10-12 21:52:04 +05:30
``` ruby
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
2019-10-12 21:52:04 +05:30
``` ruby
[:hello, "world", :this, :crushes, "an entire", "hash"]
```
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
## [`Override`](https://gitlab.com/gitlab-org/gitlab-foss/blob/master/lib/gitlab/utils/override.rb)
2018-03-17 18:26:18 +05:30
2019-03-02 22:35:43 +05:30
- This utility could help us check if a particular method would override
2018-03-17 18:26:18 +05:30
another method or not. It has the same idea of Java's `@Override` annotation
or Scala's `override` keyword. However we only do this check when
`ENV['STATIC_VERIFICATION']` is set to avoid production runtime overhead.
This is useful to check:
2019-10-12 21:52:04 +05:30
- If we have typos in overriding methods.
- If we renamed the overridden methods, making original overriding methods
overrides nothing.
2018-03-17 18:26:18 +05:30
Here's a simple example:
``` ruby
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:
``` ruby
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
```
2019-12-04 20:38:33 +05:30
## [`StrongMemoize`](https://gitlab.com/gitlab-org/gitlab-foss/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-10-12 21:52:04 +05:30
We often do `@value ||= compute`, however this doesn't work well if
`compute` might eventually give `nil` and we don't want to compute again.
Instead we could use `defined?` to check if the value is set or not.
However it's tedious to write such pattern, and `StrongMemoize` would
help us 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
2019-10-12 21:52:04 +05:30
``` ruby
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-10-12 21:52:04 +05:30
We could write it like:
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
``` ruby
class Find
include Gitlab::Utils::StrongMemoize
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
def result
strong_memoize(:result) do
search
2018-03-17 18:26:18 +05:30
end
end
2019-10-12 21:52:04 +05:30
end
```
2018-03-17 18:26:18 +05:30
2019-03-02 22:35:43 +05:30
- Clear memoization
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
``` ruby
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-04 20:38:33 +05:30
## [`RequestCache`](https://gitlab.com/gitlab-org/gitlab-foss/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.
A simple example that only uses the instance level customised values:
``` ruby
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
currently active, then it would be stored in a hash saved in an
instance variable, so the cache logic would be the same.
We can also set different strategies for different methods:
``` ruby
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
```