summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2019-09-04 09:14:24 +0100
committerNick Thomas <nick@gitlab.com>2019-09-10 13:45:21 +0100
commit6ce21a9c1712c4c64b628fd65207c2f8eb7fc39d (patch)
tree2e9a7734fd35da00465a256b4a6e413731fd07cf
parent96a9a140e8382997687a7a10afc616a8fb8281ed (diff)
downloadgitlab-ce-6ce21a9c1712c4c64b628fd65207c2f8eb7fc39d.tar.gz
Add a predicate to check for strong memoization
-rw-r--r--lib/gitlab/utils/strong_memoize.rb6
-rw-r--r--spec/lib/gitlab/utils/strong_memoize_spec.rb16
2 files changed, 21 insertions, 1 deletions
diff --git a/lib/gitlab/utils/strong_memoize.rb b/lib/gitlab/utils/strong_memoize.rb
index 3021a91dd83..483bfe12c68 100644
--- a/lib/gitlab/utils/strong_memoize.rb
+++ b/lib/gitlab/utils/strong_memoize.rb
@@ -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
diff --git a/spec/lib/gitlab/utils/strong_memoize_spec.rb b/spec/lib/gitlab/utils/strong_memoize_spec.rb
index 26baaf873a8..624e799c5e9 100644
--- a/spec/lib/gitlab/utils/strong_memoize_spec.rb
+++ b/spec/lib/gitlab/utils/strong_memoize_spec.rb
@@ -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' }