Sha256: 263abe4e2dd4148e13edd699b2ae7ed846728734f67b329fe3f9f658e5f9481d

Contents?: true

Size: 1.94 KB

Versions: 4

Compression:

Stored size: 1.94 KB

Contents

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
infopark_fiona7-1.6.1.1.5 lib/fiona7/widget_resolver.rb
infopark_fiona7-1.6.1.1.1 lib/fiona7/widget_resolver.rb
infopark_fiona7-1.6.1.1.0 lib/fiona7/widget_resolver.rb
infopark_fiona7-1.5.5.5.1 lib/fiona7/widget_resolver.rb