Class: Sunspot::Adapters::InstanceAdapter
- Inherits:
-
Object
- Object
- Sunspot::Adapters::InstanceAdapter
- Defined in:
- lib/sunspot/adapters.rb
Overview
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.(@instance.path)
end
end
# then in your initializer
Sunspot::Adapters::InstanceAdapter.register(FileAdapter, File)
Class Method Summary collapse
-
.adapt(instance) ⇒ Object
Instantiate an InstanceAdapter for the given object, searching for registered adapters for the object’s class.
-
.for(clazz) ⇒ Object
Find the best InstanceAdapter implementation that adapts the given class.
-
.index_id_for(class_name, id) ⇒ Object
:nodoc:.
-
.register(instance_adapter, *classes) ⇒ Object
Register an instance adapter for a set of classes.
-
.registered_adapter_for(clazz) ⇒ Object
Returns the directly-registered adapter for the specified class, if one exists, without searching the class’s ancestors.
Instance Method Summary collapse
-
#index_id ⇒ Object
The universally-unique ID for this instance that will be stored in solr.
-
#initialize(instance) ⇒ InstanceAdapter
constructor
:nodoc:.
Constructor Details
#initialize(instance) ⇒ InstanceAdapter
:nodoc:
54 55 56 |
# File 'lib/sunspot/adapters.rb', line 54 def initialize(instance) #:nodoc: @instance = instance end |
Class Method Details
.adapt(instance) ⇒ Object
Instantiate an InstanceAdapter for the given object, searching for registered adapters for the object’s class.
Parameters
- instance<Object>
-
The instance to adapt
Returns
- InstanceAdapter
-
An instance of an InstanceAdapter implementation that wraps the given instance
83 84 85 86 87 88 89 |
# File 'lib/sunspot/adapters.rb', line 83 def adapt(instance) #:nodoc: @known_adapters ||= {} clazz = instance.class adapter = @known_adapters[clazz.name.to_sym] || self.for(clazz) @known_adapters[clazz.name.to_sym] ||= adapter adapter.new(instance) end |
.for(clazz) ⇒ Object
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
124 125 126 127 128 129 |
# 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 |
.index_id_for(class_name, id) ⇒ Object
:nodoc:
148 149 150 |
# File 'lib/sunspot/adapters.rb', line 148 def index_id_for(class_name, id) #:nodoc: "#{class_name} #{id}" end |
.register(instance_adapter, *classes) ⇒ Object
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
102 103 104 105 106 |
# 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) ⇒ Object
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
143 144 145 146 |
# 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 |
Instance Method Details
#index_id ⇒ Object
The universally-unique ID for this instance that will be stored in solr
Returns
- String
-
ID for use in Solr
65 66 67 |
# File 'lib/sunspot/adapters.rb', line 65 def index_id #:nodoc: InstanceAdapter.index_id_for(@instance.class.name, id) end |