Commit 6ce21a9c authored by Nick Thomas's avatar Nick Thomas

Add a predicate to check for strong memoization

parent 96a9a140
...@@ -24,13 +24,17 @@ module Gitlab ...@@ -24,13 +24,17 @@ module Gitlab
# end # end
# #
def strong_memoize(name) def strong_memoize(name)
if instance_variable_defined?(ivar(name)) if strong_memoized?(name)
instance_variable_get(ivar(name)) instance_variable_get(ivar(name))
else else
instance_variable_set(ivar(name), yield) instance_variable_set(ivar(name), yield)
end end
end end
def strong_memoized?(name)
instance_variable_defined?(ivar(name))
end
def clear_memoization(name) def clear_memoization(name)
remove_instance_variable(ivar(name)) if instance_variable_defined?(ivar(name)) remove_instance_variable(ivar(name)) if instance_variable_defined?(ivar(name))
end end
......
...@@ -52,6 +52,22 @@ describe Gitlab::Utils::StrongMemoize do ...@@ -52,6 +52,22 @@ describe Gitlab::Utils::StrongMemoize do
end end
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 describe '#clear_memoization' do
let(:value) { 'mepmep' } let(:value) { 'mepmep' }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment