Add a predicate to check for strong memoization

This commit is contained in:
Nick Thomas 2019-09-04 09:14:24 +01:00
parent 96a9a140e8
commit 6ce21a9c17
No known key found for this signature in database
GPG Key ID: 2A313A47AFADACE9
2 changed files with 21 additions and 1 deletions

View File

@ -24,13 +24,17 @@ module Gitlab
# end
#
def strong_memoize(name)
if instance_variable_defined?(ivar(name))
if strong_memoized?(name)
instance_variable_get(ivar(name))
else
instance_variable_set(ivar(name), yield)
end
end
def strong_memoized?(name)
instance_variable_defined?(ivar(name))
end
def clear_memoization(name)
remove_instance_variable(ivar(name)) if instance_variable_defined?(ivar(name))
end

View File

@ -52,6 +52,22 @@ describe Gitlab::Utils::StrongMemoize do
end
end
describe '#strong_memoized?' do
let(:value) { :anything }
subject { object.strong_memoized?(:method_name) }
it 'returns false if the value is uncached' do
is_expected.to be(false)
end
it 'returns true if the value is cached' do
object.method_name
is_expected.to be(true)
end
end
describe '#clear_memoization' do
let(:value) { 'mepmep' }