Sha256: bf3812c2fc442f8417c153de483fa47b54889c8e3e43da0466146fef3b2c5fef

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 KB

Contents

module Sunspot
  #
  # InstantiatedFacet instances allow access to a model instance based on a
  # primary key stored in facet rows' values. The rows are hydrated lazily, but
  # all rows are hydrated the first time #instance is called on any of the rows.
  #
  # The #rows method returns InstantiatedFacetRow objects.
  #
  class InstantiatedFacet < Facet
    # 
    # Hydrate all rows for the facet. For data accessors that can efficiently
    # batch load, this is more efficient than individually lazy-loading
    # instances for each row, but allows us to still stay lazy and not do work
    # in the persistent store if the instances are not needed.
    #
    def populate_instances! #:nodoc:
      ids = rows.map { |row| row.value }
      reference_class = Sunspot::Util.full_const_get(@field.reference.to_s)
      accessor = Adapters::DataAccessor.create(reference_class)
      instance_map = accessor.load_all(ids).inject({}) do |map, instance|
        map[Adapters::InstanceAdapter.adapt(instance).id] = instance
        map
      end
      for row in rows
        row.instance = instance_map[row.value]
      end
    end

    private

    # 
    # Override the Facet#new_row method to return an InstantiateFacetRow
    #
    def new_row(pair)
      InstantiatedFacetRow.new(pair, self)
    end
  end
end

Version data entries

11 entries across 11 versions & 4 rubygems

Version Path
Chrononaut-sunspot-client-0.9.4 lib/sunspot/instantiated_facet.rb
UnderpantsGnome-sunspot-0.9.1.1 lib/sunspot/instantiated_facet.rb
kuahyeow-sunspot-0.9.7 lib/sunspot/instantiated_facet.rb
kuahyeow-sunspot-0.9.8 lib/sunspot/instantiated_facet.rb
outoftime-sunspot-0.9.0 lib/sunspot/instantiated_facet.rb
outoftime-sunspot-0.9.1 lib/sunspot/instantiated_facet.rb
outoftime-sunspot-0.9.2 lib/sunspot/instantiated_facet.rb
outoftime-sunspot-0.9.3 lib/sunspot/instantiated_facet.rb
outoftime-sunspot-0.9.4 lib/sunspot/instantiated_facet.rb
outoftime-sunspot-0.9.5 lib/sunspot/instantiated_facet.rb
outoftime-sunspot-0.9.6 lib/sunspot/instantiated_facet.rb