class Sunspot::Adapters::DataAccessor

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.

Example

class FileAccessor < Sunspot::Adapters::InstanceAdapter
  def load(id)
    @clazz.open(id)
  end
end

Sunspot::Adapters::DataAccessor.register(FileAccessor, File)

Attributes

clazz[R]
inherited_attributes[RW]

Attributes that should be passed to other adapted subclasses

Public Instance Methods

load_all(ids) click to toggle source

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.

Parameters

ids<Array>

collection of IDs

Returns

Array

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

Public Class Methods

register(data_accessor, *classes) click to toggle source

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.

Parameters

data_accessor<Class>

The data accessor class to register

classes…<Class>

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
registered_accessor_for(clazz) click to toggle source

Returns the directly-registered accessor for the specified class, if one exists, without searching the class’s ancestors.

Parameters

clazz<Class>

The model class to be checked for the registered

data accessor

Returns

Class

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