Class: Sunspot::Adapters::DataAccessor

Inherits:
Object
  • Object
show all
Defined in:
sunspot/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)

Direct Known Subclasses

Rails::Adapters::ActiveRecordDataAccessor

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (DataAccessor) initialize(clazz)

:nodoc:



169
170
171
# File 'sunspot/lib/sunspot/adapters.rb', line 169

def initialize(clazz) #:nodoc:
  @clazz = clazz
end

Class Method Details

+ (Object) create(clazz)

Create a DataAccessor for the given class, searching registered adapters for the best match. See InstanceAdapter#adapt for discussion of inheritence.

Parameters

clazz

Class to create DataAccessor for

Returns

DataAccessor

DataAccessor implementation which provides access to given class



203
204
205
# File 'sunspot/lib/sunspot/adapters.rb', line 203

def create(clazz) #:nodoc:
  self.for(clazz).new(clazz)
end

+ (Object) for(clazz)

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



240
241
242
243
244
245
246
247
248
249
# File 'sunspot/lib/sunspot/adapters.rb', line 240

def for(clazz) #:nodoc:
  original_class_name = clazz.name
  clazz.ancestors.each do |ancestor_class|
    next if ancestor_class.name.nil? || ancestor_class.name.empty?
    class_name = ancestor_class.name.to_sym
    return data_accessors[class_name] if data_accessors[class_name]
  end
  raise(Sunspot::NoAdapterError,
        "No data accessor is configured for #{original_class_name} or its superclasses. See the documentation for Sunspot::Adapters")
end

+ (Object) register(data_accessor, *classes)

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 providess access to



218
219
220
221
222
# File 'sunspot/lib/sunspot/adapters.rb', line 218

def register(data_accessor, *classes)
  classes.each do |clazz|
    data_accessors[clazz.name.to_sym] = data_accessor
  end
end

Instance Method Details

- (Object) load_all(ids)

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



185
186
187
# File 'sunspot/lib/sunspot/adapters.rb', line 185

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