Module: Sunspot::Field::FieldInstance

Included in:
DynamicFieldInstance, StaticField
Defined in:
lib/sunspot/field.rb

Overview

The FieldInstance module encapsulates functionality associated with acting as a concrete instance of a field for the purposes of search. In particular, FieldInstances need to be able to return indexed names, convert values to their indexed representation, and cast returned values to the appropriate native Ruby type.

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object

Cast the value into the appropriate Ruby class for the field’s type

Parameters

value<String>

Solr’s representation of the value

Returns

Object

The cast value



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

def cast(value)
  @type.cast(value)
end

#indexed_nameObject

The name of the field as it is indexed in Solr. The indexed name contains a suffix that contains information about the type as well as whether the field allows multiple values for a document.

Returns

String

The field’s indexed name



37
38
39
# File 'lib/sunspot/field.rb', line 37

def indexed_name
  "#{@type.indexed_name(name)}#{'m' if @multiple}"
end

#to_indexed(value) ⇒ Object

Convert a value to its representation for Solr indexing. This delegates to the #to_indexed method on the field’s type.

Parameters

value<Object>

Value to convert to Solr representation

Returns

String

Solr representation of the object

Raises

ArgumentError

the value is an array, but this field does not allow multiple values



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sunspot/field.rb', line 57

def to_indexed(value)
  if value.is_a? Array
    if @multiple
      value.map { |val| to_indexed(val) }
    else
      raise ArgumentError, "#{name} is not a multiple-value field, so it cannot index values #{value.inspect}"
    end
  else
    @type.to_indexed(value)
  end
end