class Sunspot::Adapters::InstanceAdapter

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.

Example:

class FileAdapter < Sunspot::Adapters::InstanceAdapter
  def id
    File.expand_path(@instance.path)
  end
end

# then in your initializer
Sunspot::Adapters::InstanceAdapter.register(FileAdapter, File)

Public Class Methods

for(clazz) click to toggle source

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.

Parameters

clazz<Class>

The class to find an InstanceAdapter for

Returns

Class

Subclass of InstanceAdapter, or nil if none found

Raises

Sunspot::NoAdapterError

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(instance_adapter, *classes) click to toggle source

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.

Parameters

instance_adapter<Class>

The instance adapter class to register

classes…<Class>

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

Returns the directly-registered adapter 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

adapter

Returns

Class

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