module Fiona7 class WidgetResolver def initialize(widget_pool_value, obj_repository) self.value = widget_pool_value self.obj_repository = obj_repository self.preload end attr_reader :id_map, :path_map, :instance_map def all self.instance_map.values end protected attr_accessor :value, :obj_repository attr_writer :id_map, :path_map, :instance_map def preload widget_id_to_object_id_map = Hash[ self.value.map {|link| [link["title"], link["destination"]] } ] object_id_to_widget_instance_map = Hash[ self.obj_repository.find_many(widget_id_to_object_id_map.values).compact.map {|obj| [obj.id, obj] } ] self.id_map = widget_id_to_object_id_map.dup self.path_map = Hash[ widget_id_to_object_id_map.map {|widget_id, obj_id| path = object_id_to_widget_instance_map[obj_id].try(:path) # NOTE: with garbage collection some widget objects may become # invisible: that is they are still in the database, but unreadable # when querying for released contents (because reverted for example) # This results in a "dead" link in the widget pool, but shouldn't # therwise be harmful next unless path [widget_id, object_id_to_widget_instance_map[obj_id].try(:path)] }.compact ] self.instance_map = Hash[ widget_id_to_object_id_map.map {|widget_id, obj_id| object = object_id_to_widget_instance_map[obj_id] # NOTE: with garbage collection some widget objects may become # invisible: that is they are still in the database, but unreadable # when querying for released contents (because reverted for example) # This results in a "dead" link in the widget pool, but shouldn't # therwise be harmful next unless object [widget_id, object] }.compact ] end end end