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 retreiving 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)
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.
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:
164 165 166 |
# File 'lib/sunspot/adapters.rb', line 164 def initialize(clazz) #:nodoc: @clazz = clazz 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 inheritence.
Parameters
- clazz<Class>
-
Class to create DataAccessor for
Returns
- DataAccessor
-
DataAccessor implementation which provides access to given class
198 199 200 |
# File 'lib/sunspot/adapters.rb', line 198 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, or nil if none found
231 232 233 234 235 236 237 |
# File 'lib/sunspot/adapters.rb', line 231 def for(clazz) #:nodoc: while clazz != Object class_name = clazz.name.to_sym return data_accessors[class_name] if data_accessors[class_name] clazz = clazz.superclass end 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 providess access to
213 214 215 216 217 |
# File 'lib/sunspot/adapters.rb', line 213 def register(data_accessor, *classes) for clazz in classes data_accessors[clazz.name.to_sym] = data_accessor end 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
180 181 182 |
# File 'lib/sunspot/adapters.rb', line 180 def load_all(ids) ids.map { |id| self.load(id) } end |