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(FileAdapter, File)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ InstanceAdapter

:nodoc:



54
55
56
# File 'lib/sunspot/adapters.rb', line 54

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

The instance to adapt

Returns

InstanceAdapter

An instance of an InstanceAdapter implementation that wraps the given instance



97
98
99
100
101
102
103
# File 'lib/sunspot/adapters.rb', line 97

def adapt(instance) #:nodoc:
  @known_adapters ||= {}
  clazz = instance.class
  adapter = @known_adapters[clazz.name.to_sym] || self.for(clazz)
  @known_adapters[clazz.name.to_sym] ||= adapter
  adapter.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

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



138
139
140
141
142
143
# File 'lib/sunspot/adapters.rb', line 138

def for(clazz)
  adapter = registered_adapter_for(clazz) || registered_adapter_for_ancestors_of(clazz)
  return adapter if adapter
  raise(Sunspot::NoAdapterError,
        "No adapter is configured for #{clazz.name} or its superclasses. See the documentation for Sunspot::Adapters")
end

.index_id_for(class_name, id) ⇒ Object

:nodoc:



162
163
164
# File 'lib/sunspot/adapters.rb', line 162

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

The instance adapter class to register

classes...

One or more classes that this instance adapter adapts



116
117
118
119
120
# File 'lib/sunspot/adapters.rb', line 116

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

.registered_adapter_for(clazz) ⇒ Object

Returns the directly-registered adapter for the specified class, if one exists, without searching the class's ancestors.

Parameters

clazz

The model class to be checked for the registered adapter

Returns

Class

Subclass of InstanceAdapter, or nil if none found



157
158
159
160
# File 'lib/sunspot/adapters.rb', line 157

def registered_adapter_for(clazz)
  return nil if clazz.name.nil? || clazz.name.empty?
  instance_adapters[clazz.name.to_sym]
end

Instance Method Details

#id_prefixObject

An ID prefix to be added to the index_id

Returns

String

ID prefix for use in index ID to determine

the shard a document is sent to for indexing



66
67
68
69
70
# File 'lib/sunspot/adapters.rb', line 66

def id_prefix
  setup = Sunspot::Setup.for(@instance.class)

  setup && setup.id_prefix_for(@instance)
end

#index_idObject

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

Returns

String

ID for use in Solr



79
80
81
# File 'lib/sunspot/adapters.rb', line 79

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