Class: Sunspot::Adapters::DataAccessor

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clazz) ⇒ DataAccessor

:nodoc:



215
216
217
218
# File 'lib/sunspot/adapters.rb', line 215

def initialize(clazz) #:nodoc:
  @clazz = clazz
  @inherited_attributes = []
end

Instance Attribute Details

#clazzObject (readonly)

Returns the value of attribute clazz.



213
214
215
# File 'lib/sunspot/adapters.rb', line 213

def clazz
  @clazz
end

#inherited_attributesObject

Attributes that should be passed to other adapted subclasses



212
213
214
# File 'lib/sunspot/adapters.rb', line 212

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 to create DataAccessor for

Returns

DataAccessor

DataAccessor implementation which provides access to given class



250
251
252
# File 'lib/sunspot/adapters.rb', line 250

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

The class to find a DataAccessor for

Returns

Class

Implementation of DataAccessor

Raises

Sunspot::NoAdapterError:: If no data accessor exists for the given class



287
288
289
290
291
292
# File 'lib/sunspot/adapters.rb', line 287

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

The data accessor class to register

classes...

One or more classes that this data accessor provides access to



265
266
267
268
269
# File 'lib/sunspot/adapters.rb', line 265

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

The model class to be checked for the registered data accessor

Returns

Class

Subclass of DataAccessor, or nil if none found



306
307
308
309
# File 'lib/sunspot/adapters.rb', line 306

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

collection of IDs

Returns

Array

collection of instances, in order of IDs given



232
233
234
# File 'lib/sunspot/adapters.rb', line 232

def load_all(ids)
  ids.map { |id| self.load(id) }
end