Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
258bf3e1
Commit
258bf3e1
authored
Nov 13, 2017
by
Lin Jen-Shin (godfat)
Committed by
Rémy Coutable
Nov 13, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Gitlab::Utils::StrongMemoize
parent
4a1e8188
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
93 additions
and
6 deletions
+93
-6
app/services/ci/pipeline_trigger_service.rb
app/services/ci/pipeline_trigger_service.rb
+5
-3
lib/api/api_guard.rb
lib/api/api_guard.rb
+5
-3
lib/gitlab/utils/strong_memoize.rb
lib/gitlab/utils/strong_memoize.rb
+31
-0
spec/lib/gitlab/utils/strong_memoize_spec.rb
spec/lib/gitlab/utils/strong_memoize_spec.rb
+52
-0
No files found.
app/services/ci/pipeline_trigger_service.rb
View file @
258bf3e1
module
Ci
module
Ci
class
PipelineTriggerService
<
BaseService
class
PipelineTriggerService
<
BaseService
include
Gitlab
::
Utils
::
StrongMemoize
def
execute
def
execute
if
trigger_from_token
if
trigger_from_token
create_pipeline_from_trigger
(
trigger_from_token
)
create_pipeline_from_trigger
(
trigger_from_token
)
...
@@ -26,9 +28,9 @@ module Ci
...
@@ -26,9 +28,9 @@ module Ci
end
end
def
trigger_from_token
def
trigger_from_token
return
@trigger
if
defined?
(
@trigger
)
strong_memoize
(
:trigger
)
do
Ci
::
Trigger
.
find_by_token
(
params
[
:token
].
to_s
)
@trigger
=
Ci
::
Trigger
.
find_by_token
(
params
[
:token
].
to_s
)
end
end
end
def
create_pipeline_variables!
(
pipeline
)
def
create_pipeline_variables!
(
pipeline
)
...
...
lib/api/api_guard.rb
View file @
258bf3e1
...
@@ -42,6 +42,8 @@ module API
...
@@ -42,6 +42,8 @@ module API
# Helper Methods for Grape Endpoint
# Helper Methods for Grape Endpoint
module
HelperMethods
module
HelperMethods
include
Gitlab
::
Utils
::
StrongMemoize
def
find_current_user!
def
find_current_user!
user
=
find_user_from_access_token
||
find_user_from_warden
user
=
find_user_from_access_token
||
find_user_from_warden
return
unless
user
return
unless
user
...
@@ -52,9 +54,9 @@ module API
...
@@ -52,9 +54,9 @@ module API
end
end
def
access_token
def
access_token
return
@access_token
if
defined?
(
@access_token
)
strong_memoize
(
:access_token
)
do
find_oauth_access_token
||
find_personal_access_token
@access_token
=
find_oauth_access_token
||
find_personal_access_token
end
end
end
def
validate_access_token!
(
scopes:
[])
def
validate_access_token!
(
scopes:
[])
...
...
lib/gitlab/utils/strong_memoize.rb
0 → 100644
View file @
258bf3e1
module
Gitlab
module
Utils
module
StrongMemoize
# Instead of writing patterns like this:
#
# def trigger_from_token
# return @trigger if defined?(@trigger)
#
# @trigger = Ci::Trigger.find_by_token(params[:token].to_s)
# end
#
# We could write it like:
#
# def trigger_from_token
# strong_memoize(:trigger) do
# Ci::Trigger.find_by_token(params[:token].to_s)
# end
# end
#
def
strong_memoize
(
name
)
ivar_name
=
"@
#{
name
}
"
if
instance_variable_defined?
(
ivar_name
)
instance_variable_get
(
ivar_name
)
else
instance_variable_set
(
ivar_name
,
yield
)
end
end
end
end
end
spec/lib/gitlab/utils/strong_memoize_spec.rb
0 → 100644
View file @
258bf3e1
require
'spec_helper'
describe
Gitlab
::
Utils
::
StrongMemoize
do
let
(
:klass
)
do
struct
=
Struct
.
new
(
:value
)
do
def
method_name
strong_memoize
(
:method_name
)
do
trace
<<
value
value
end
end
def
trace
@trace
||=
[]
end
end
struct
.
include
(
described_class
)
struct
end
subject
(
:object
)
{
klass
.
new
(
value
)
}
shared_examples
'caching the value'
do
it
'only calls the block once'
do
value0
=
object
.
method_name
value1
=
object
.
method_name
expect
(
value0
).
to
eq
(
value
)
expect
(
value1
).
to
eq
(
value
)
expect
(
object
.
trace
).
to
contain_exactly
(
value
)
end
it
'returns and defines the instance variable for the exact value'
do
returned_value
=
object
.
method_name
memoized_value
=
object
.
instance_variable_get
(
:@method_name
)
expect
(
returned_value
).
to
eql
(
value
)
expect
(
memoized_value
).
to
eql
(
value
)
end
end
describe
'#strong_memoize'
do
[
nil
,
false
,
true
,
'value'
,
0
,
[
0
]].
each
do
|
value
|
context
"with value
#{
value
}
"
do
let
(
:value
)
{
value
}
it_behaves_like
'caching the value'
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment