diff options
Diffstat (limited to 'lib/gitlab/utils/strong_memoize.rb')
-rw-r--r-- | lib/gitlab/utils/strong_memoize.rb | 44 |
1 files changed, 16 insertions, 28 deletions
diff --git a/lib/gitlab/utils/strong_memoize.rb b/lib/gitlab/utils/strong_memoize.rb index 50b8428113d..6456ad08924 100644 --- a/lib/gitlab/utils/strong_memoize.rb +++ b/lib/gitlab/utils/strong_memoize.rb @@ -30,10 +30,10 @@ module Gitlab # end # strong_memoize_attr :trigger_from_token # - # strong_memoize_attr :enabled?, :enabled # def enabled? # Feature.enabled?(:some_feature) # end + # strong_memoize_attr :enabled?, :enabled # def strong_memoize(name) key = ivar(name) @@ -45,6 +45,16 @@ module Gitlab end end + def strong_memoize_with(name, *args) + container = strong_memoize(name) { {} } + + if container.key?(args) + container[args] + else + container[args] = yield + end + end + def strong_memoized?(name) instance_variable_defined?(ivar(name)) end @@ -58,23 +68,8 @@ module Gitlab def strong_memoize_attr(method_name, member_name = nil) member_name ||= method_name - if method_defined?(method_name) || private_method_defined?(method_name) - StrongMemoize.send( # rubocop:disable GitlabSecurity/PublicSend - :do_strong_memoize, self, method_name, member_name) - else - StrongMemoize.send( # rubocop:disable GitlabSecurity/PublicSend - :queue_strong_memoize, self, method_name, member_name) - end - end - - def method_added(method_name) - super - - if member_name = StrongMemoize - .send(:strong_memoize_queue, self).delete(method_name) # rubocop:disable GitlabSecurity/PublicSend - StrongMemoize.send( # rubocop:disable GitlabSecurity/PublicSend - :do_strong_memoize, self, method_name, member_name) - end + StrongMemoize.send( # rubocop:disable GitlabSecurity/PublicSend + :do_strong_memoize, self, method_name, member_name) end end @@ -88,9 +83,10 @@ module Gitlab # # Depending on a type ensure that there's a single memory allocation def ivar(name) - if name.is_a?(Symbol) + case name + when Symbol name.to_s.prepend("@").to_sym - elsif name.is_a?(String) + when String :"@#{name}" else raise ArgumentError, "Invalid type of '#{name}'" @@ -100,14 +96,6 @@ module Gitlab class <<self private - def strong_memoize_queue(klass) - klass.instance_variable_get(:@strong_memoize_queue) || klass.instance_variable_set(:@strong_memoize_queue, {}) - end - - def queue_strong_memoize(klass, method_name, member_name) - strong_memoize_queue(klass)[method_name] = member_name - end - def do_strong_memoize(klass, method_name, member_name) method = klass.instance_method(method_name) |