Class: Sunspot::Adapters::InstanceAdapter

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ InstanceAdapter

:nodoc:



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

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

Class Method Details

.adapt(instance) ⇒ Object

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

Parameters

instance<Object>

The instance to adapt

Returns

InstanceAdapter

An instance of an InstanceAdapter implementation that wraps the given instance



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

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

.for(clazz) ⇒ Object

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<Class>

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 '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

.index_id_for(class_name, id) ⇒ Object

:nodoc:



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

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

.register(instance_adapter, *classes) ⇒ Object

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<Class>

The instance adapter class to register

classes…<Class>

One or more classes that this instance adapter adapts



96
97
98
99
100
# File '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

#index_idObject

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

Returns

String

ID for use in Solr



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

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