Subclasses of the DataAccessor class take care of retrieving instances of the adapted class from (usually persistent) storage. Subclasses must implement the load method, which takes an id (the value returned by InstanceAdapter#id, as a string), and returns the instance referenced by that ID. Optionally, it can also override the load_all method, which takes an array of IDs and returns an array of instances in the order given. load_all need only be implemented if it can be done more efficiently than simply iterating over the IDs and calling load on each individually.
class FileAccessor < Sunspot::Adapters::InstanceAdapter def load(id) @clazz.open(id) end end Sunspot::Adapters::DataAccessor.register(FileAccessor, File)
Attributes that should be passed to other adapted subclasses
Subclasses can override this class to provide more efficient bulk loading of instances. Instances must be returned in the same order that the IDs were given.
collection of IDs
collection of instances, in order of IDs given
# File lib/sunspot/adapters.rb, line 218 def load_all(ids) ids.map { |id| self.load(id) } end
Register data accessor for a set of classes. When searching for an accessor for a given class, Sunspot starts with the class, and then searches for registered adapters up the class’s ancestor chain.
The data accessor class to register
One or more classes that this data accessor provides access to
# File lib/sunspot/adapters.rb, line 251 def register(data_accessor, *classes) classes.each do |clazz| data_accessors[clazz.name.to_sym] = data_accessor end end
Returns the directly-registered accessor for the specified class, if one exists, without searching the class’s ancestors.
The model class to be checked for the registered
data accessor
Subclass of DataAccessor, or nil if none found
# File lib/sunspot/adapters.rb, line 292 def registered_accessor_for(clazz) return nil if clazz.name.nil? || clazz.name.empty? data_accessors[clazz.name.to_sym] end