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, LocationType, LongType, StringType, TextType, TimeType, TrieFloatType, TrieIntegerType, TrieTimeType

Class Method Summary collapse

Class Method Details

.for(object) ⇒ Object



40
41
42
# File 'lib/sunspot/type.rb', line 40

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

.for_class(clazz) ⇒ Object



34
35
36
37
38
# File 'lib/sunspot/type.rb', line 34

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

.register(sunspot_type, *classes) ⇒ Object



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

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



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

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

.to_literal(object) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/sunspot/type.rb', line 52

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