Module: Sunspot::Adapters

Defined in:
sunspot/lib/sunspot/adapters.rb

Overview

Sunspot works by saving references to the primary key (or natural ID) of each indexed object, and then retrieving the objects from persistent storage when their IDs are referenced in search results. In order for Sunspot to know what an object’s primary key is, and how to retrieve objects from persistent storage given a primary key, an adapter must be registered for that object’s class or one of its superclasses (for instance, an adapter registered for ActiveRecord::Base would be used for all ActiveRecord models).

To provide Sunspot with this ability, adapters must have two roles:

Data accessor

A subclass of Sunspot::Adapters::DataAccessor, this object is instantiated with a particular class and must respond to the #load() method, which returns an object from persistent storage given that object’s primary key. It can also optionally implement the #load_all() method, which returns a collection of objects given a collection of primary keys, if that can be done more efficiently than calling #load() on each key.

Instance adapter

A subclass of Sunspot::Adapters::InstanceAdapter, this object is instantiated with a particular instance. Its only job is to tell Sunspot what the object’s primary key is, by implementing the #id() method.

Adapters are registered by registering their two components, telling Sunspot that they are available for one or more classes, and all of their subclasses. See Sunspot::Adapters::DataAccessor.register and Sunspot::Adapters::InstanceAdapter.register for the details.

See spec/mocks/mock_adapter.rb for an example of how adapter classes should be implemented.

Defined Under Namespace

Classes: DataAccessor, InstanceAdapter