module Fiona7 class WidgetResolver def initialize(widget_pool_value, klass=WriteObj) self.value = widget_pool_value self.klass = klass preload end attr_reader :id_map, :path_map, :instance_map protected attr_accessor :value, :klass 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.klass.where(obj_id: widget_id_to_object_id_map.values).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| [widget_id, object_id_to_widget_instance_map[obj_id].path] } ] self.instance_map = Hash[ widget_id_to_object_id_map.map {|widget_id, obj_id| [widget_id, object_id_to_widget_instance_map[obj_id]] } ] end end end