utilities.md 4,2 КБ
Newer Older
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
1
2
3
4
5
6
# GitLab utilities

We developed a number of utilities to ease development.

## [`MergeHash`](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/utils/merge_hash.rb)

Evan Read's avatar
Evan Read включено в состав коммита
7
- Deep merges an array of hashes:
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

    ``` 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"
    ]
    ```

Evan Read's avatar
Evan Read включено в состав коммита
34
- Extracts all keys and values from a hash into an array:
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
35
36
37
38
39
40
41
42
43
44
45
46
47

    ``` ruby
    Gitlab::Utils::MergeHash.crush(
      { hello: "world", this: { crushes: ["an entire", "hash"] } }
    )
    ```

    Gives:

    ``` ruby
    [:hello, "world", :this, :crushes, "an entire", "hash"]
    ```

Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
48
49
## [`Override`](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/utils/override.rb)

Evan Read's avatar
Evan Read включено в состав коммита
50
- This utility could help us check if a particular method would override
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
51
52
53
54
55
  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:

Evan Read's avatar
Evan Read включено в состав коммита
56
57
    - If we have typos in overriding methods.
    - If we renamed the overridden methods, making original overriding methods
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
      overrides nothing.

    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
    ```

Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
93
94
## [`StrongMemoize`](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/utils/strong_memoize.rb)

Evan Read's avatar
Evan Read включено в состав коммита
95
- Memoize the value even if it is `nil` or `false`.
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

    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.

    Instead of writing patterns like this:

    ``` ruby
    class Find
      def result
        return @result if defined?(@result)

        @result = search
      end
    end
    ```

    We could write it like:

    ``` ruby
    class Find
      include Gitlab::Utils::StrongMemoize

      def result
        strong_memoize(:result) do
          search
        end
      end
    end
    ```

Evan Read's avatar
Evan Read включено в состав коммита
129
- Clear memoization
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
130
131
132
133
134
135
136
137

    ``` ruby
    class Find
      include Gitlab::Utils::StrongMemoize
    end

    Find.new.clear_memoization(:result)
    ```
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

## [`RequestCache`](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/cache/request_cache.rb)

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
Tiago Botelho's avatar
Tiago Botelho включено в состав коммита
174
    User.find_by_any_email(author_email)
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
175
  end
Tiago Botelho's avatar
Tiago Botelho включено в состав коммита
176
  request_cache(:author) { author_email }
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
177
178
end
```