Class: Sunspot::Adapters::InstanceAdapter

Inherits:
Object
  • Object
show all
Defined in:
sunspot/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.expand_path(@instance.path)
    end
  end

  # then in your initializer
  Sunspot::Adapters::InstanceAdapter.register(MyAdapter, File)

Direct Known Subclasses

Rails::Adapters::ActiveRecordInstanceAdapter

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (InstanceAdapter) initialize(instance)

:nodoc:



52
53
54
# File 'sunspot/lib/sunspot/adapters.rb', line 52

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

Class Method Details

+ (Object) adapt(instance)

Instantiate an InstanceAdapter for the given object, searching for registered adapters for the object’s class.

Parameters

instance

The instance to adapt

Returns

InstanceAdapter

An instance of an InstanceAdapter implementation that wraps the given instance



81
82
83
# File 'sunspot/lib/sunspot/adapters.rb', line 81

def adapt(instance) #:nodoc:
  self.for(instance.class).new(instance)
end

+ (Object) for(clazz)

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

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



118
119
120
121
122
123
124
125
126
127
128
# File 'sunspot/lib/sunspot/adapters.rb', line 118

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 instance_adapters[class_name] if instance_adapters[class_name]
  end

  raise(Sunspot::NoAdapterError,
        "No adapter is configured for #{original_class_name} or its superclasses. See the documentation for Sunspot::Adapters")
end

+ (Object) index_id_for(class_name, id)

:nodoc:



130
131
132
# File 'sunspot/lib/sunspot/adapters.rb', line 130

def index_id_for(class_name, id) #:nodoc:
  "#{class_name} #{id}"
end

+ (Object) register(instance_adapter, *classes)

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

The instance adapter class to register

classes…

One or more classes that this instance adapter adapts



96
97
98
99
100
# File 'sunspot/lib/sunspot/adapters.rb', line 96

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

Instance Method Details

- (Object) index_id

The universally-unique ID for this instance that will be stored in solr

Returns

String

ID for use in Solr



63
64
65
# File 'sunspot/lib/sunspot/adapters.rb', line 63

def index_id #:nodoc:
  InstanceAdapter.index_id_for(@instance.class.name, id)
end