Subclasses of the InstanceAdapter class should implement the id method, which returns the primary key of the instance stored in the @instance variable. The primary key must be unique within the scope of the instance’s class.
class FileAdapter < Sunspot::Adapters::InstanceAdapter def id File.expand_path(@instance.path) end end # then in your initializer Sunspot::Adapters::InstanceAdapter.register(FileAdapter, File)
Find the best InstanceAdapter implementation that adapts the given class. Starting with the class and then moving up the ancestor chain, looks for registered InstanceAdapter implementations.
The class to find an InstanceAdapter for
Subclass of InstanceAdapter, or nil if none found
If no adapter is registered for this class
# File lib/sunspot/adapters.rb, line 124 def for(clazz) adapter = registered_adapter_for(clazz) || registered_adapter_for_ancestors_of(clazz) return adapter if adapter raise(Sunspot::NoAdapterError, "No adapter is configured for #{clazz.name} or its superclasses. See the documentation for Sunspot::Adapters") end
Register an instance adapter for a set of classes. When searching for an adapter for a given instance, Sunspot starts with the instance’s class, and then searches for registered adapters up the class’s ancestor chain.
The instance adapter class to register
One or more classes that this instance adapter adapts
# File lib/sunspot/adapters.rb, line 102 def register(instance_adapter, *classes) classes.each do |clazz| instance_adapters[clazz.name.to_sym] = instance_adapter end end
Returns the directly-registered adapter for the specified class, if one exists, without searching the class’s ancestors.
The model class to be checked for the registered
adapter
Subclass of InstanceAdapter, or nil if none found
# File lib/sunspot/adapters.rb, line 143 def registered_adapter_for(clazz) return nil if clazz.name.nil? || clazz.name.empty? instance_adapters[clazz.name.to_sym] end