Module: Sunspot::Type

Defined in:
lib/sunspot/type.rb

Overview

This module contains singleton objects that represent the types that can be indexed and searched using Sunspot. Plugin developers should be able to add new constants to the Type module; as long as they implement the appropriate methods, Sunspot should be able to integrate them (note that this capability is untested at the moment). The required methods are:

indexed_name

Convert a given field name into its form as stored in Solr. This generally means adding a suffix to match a Solr dynamicField definition.

to_indexed

Convert a value of this type into the appropriate Solr string representation.

cast

Convert a Solr string representation of a value into the appropriate Ruby type.

Defined Under Namespace

Classes: AbstractType, BooleanType, ClassType, DateType, DoubleType, FloatType, IntegerType, LongType, StringType, TextType, TimeType, TrieFloatType, TrieIntegerType, TrieTimeType

Class Method Summary collapse

Class Method Details

.for(object) ⇒ Object



33
34
35
# File 'lib/sunspot/type.rb', line 33

def for(object)
  for_class(object.class)
end

.for_class(clazz) ⇒ Object



27
28
29
30
31
# File 'lib/sunspot/type.rb', line 27

def for_class(clazz)
  if clazz
    ruby_type_map[clazz.name.to_sym] || for_class(clazz.superclass)
  end
end

.register(sunspot_type, *classes) ⇒ Object



21
22
23
24
25
# File 'lib/sunspot/type.rb', line 21

def register(sunspot_type, *classes)
  classes.each do |clazz|
    ruby_type_map[clazz.name.to_sym] = sunspot_type.instance
  end
end

.to_indexed(object) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/sunspot/type.rb', line 37

def to_indexed(object)
  if type = self.for(object)
    type.to_indexed(object)
  else
    object.to_s
  end
end

.to_literal(object) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/sunspot/type.rb', line 45

def to_literal(object)
  if type = self.for(object)
    type.to_literal(object)
  else
    raise ArgumentError, "Can't use #{object.inspect} as Solr literal: #{object.class} has no registered Solr type"
  end
end