From 18f343779bc4fe0bb0ff68d1feae5500c20138db Mon Sep 17 00:00:00 2001 From: Jacobo Bouzas Quiroga Date: Thu, 5 Dec 2024 12:18:47 +0100 Subject: [PATCH] improvement: make method more scalable --- lib/resource_registry/registry.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/resource_registry/registry.rb b/lib/resource_registry/registry.rb index c043fc8..f50be28 100644 --- a/lib/resource_registry/registry.rb +++ b/lib/resource_registry/registry.rb @@ -49,7 +49,7 @@ def fetch!(identifier) ).returns(T.nilable(Resource)) end def fetch_for_repository(repository_class) - fetch_all.values.find { |r| r.repository_raw == repository_class.to_s } + resources_by_raw_repository[repository_class.to_s] end alias find_for_repository fetch_for_repository @@ -78,6 +78,15 @@ def fetch_with_capabilities(*capabilities) sig { returns(T::Hash[String, Resource]) } attr_accessor :resources + sig { returns(T::Hash[String, Resource]) } + def resources_by_raw_repository + @resources_by_raw_repository ||= + T.let( + resources.values.index_by(&:repository_raw), + T.nilable(T::Hash[String, Resource]) + ) + end + sig { params(resources: T::Array[Resource]).returns(T::Boolean) } def duplicated_identifier?(resources) resources.map(&:identifier).uniq.size != resources.size