Class: Sunspot::Adapters::DataAccessor
- Inherits:
-
Object
- Object
- Sunspot::Adapters::DataAccessor
- Defined in:
- lib/sunspot/adapters.rb
Overview
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)
Instance Attribute Summary collapse
-
#clazz ⇒ Object
readonly
Returns the value of attribute clazz.
-
#inherited_attributes ⇒ Object
Attributes that should be passed to other adapted subclasses.
Class Method Summary collapse
-
.create(clazz) ⇒ Object
Create a DataAccessor for the given class, searching registered adapters for the best match.
-
.for(clazz) ⇒ Object
Find the best DataAccessor implementation that adapts the given class.
-
.register(data_accessor, *classes) ⇒ Object
Register data accessor for a set of classes.
-
.registered_accessor_for(clazz) ⇒ Object
Returns the directly-registered accessor for the specified class, if one exists, without searching the class’s ancestors.
Instance Method Summary collapse
-
#initialize(clazz) ⇒ DataAccessor
constructor
:nodoc:.
-
#load_all(ids) ⇒ Object
Subclasses can override this class to provide more efficient bulk loading of instances.
Constructor Details
#initialize(clazz) ⇒ DataAccessor
:nodoc:
201 202 203 204 |
# File 'lib/sunspot/adapters.rb', line 201 def initialize(clazz) #:nodoc: @clazz = clazz @inherited_attributes = [] end |
Instance Attribute Details
#clazz ⇒ Object (readonly)
Returns the value of attribute clazz.
199 200 201 |
# File 'lib/sunspot/adapters.rb', line 199 def clazz @clazz end |
#inherited_attributes ⇒ Object
Attributes that should be passed to other adapted subclasses
198 199 200 |
# File 'lib/sunspot/adapters.rb', line 198 def inherited_attributes @inherited_attributes end |
Class Method Details
.create(clazz) ⇒ Object
Create a DataAccessor for the given class, searching registered adapters for the best match. See InstanceAdapter#adapt for discussion of inheritance.
Parameters
- clazz<Class>
-
Class to create DataAccessor for
Returns
- DataAccessor
-
DataAccessor implementation which provides access to given class
236 237 238 |
# File 'lib/sunspot/adapters.rb', line 236 def create(clazz) #:nodoc: self.for(clazz).new(clazz) end |
.for(clazz) ⇒ Object
Find the best DataAccessor implementation that adapts the given class. Starting with the class and then moving up the ancestor chain, looks for registered DataAccessor implementations.
Parameters
- clazz<Class>
-
The class to find a DataAccessor for
Returns
- Class
-
Implementation of DataAccessor
Raises
- Sunspot::NoAdapterError
-
If no data accessor exists for the given class
273 274 275 276 277 278 |
# File 'lib/sunspot/adapters.rb', line 273 def for(clazz) #:nodoc: accessor = registered_accessor_for(clazz) || registered_accessor_for_ancestors_of(clazz) return accessor if accessor raise(Sunspot::NoAdapterError, "No data accessor is configured for #{clazz.name} or its superclasses. See the documentation for Sunspot::Adapters") end |
.register(data_accessor, *classes) ⇒ Object
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
251 252 253 254 255 |
# 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) ⇒ Object
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
292 293 294 295 |
# 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 |
Instance Method Details
#load_all(ids) ⇒ Object
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
218 219 220 |
# File 'lib/sunspot/adapters.rb', line 218 def load_all(ids) ids.map { |id| self.load(id) } end |