Class: Sunspot::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/field.rb

Overview

:nodoc:

Direct Known Subclasses

AttributeField, FulltextField

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Field

Returns a new instance of Field.

[View source]

10
11
12
13
14
# File 'lib/sunspot/field.rb', line 10

def initialize(name, type, options = {}) #:nodoc
  @name, @type = name.to_sym, type
  @stored = !!options.delete(:stored)
  @attributes = {}
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.


6
7
8
# File 'lib/sunspot/field.rb', line 6

def attributes
  @attributes
end

#nameObject

The public-facing name of the field


3
4
5
# File 'lib/sunspot/field.rb', line 3

def name
  @name
end

#referenceObject

Model class that the value of this field refers to


5
6
7
# File 'lib/sunspot/field.rb', line 5

def reference
  @reference
end

#typeObject

The Type of the field


4
5
6
# File 'lib/sunspot/field.rb', line 4

def type
  @type
end

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

[View source]

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

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

#eql?(field) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)
[View source]

85
86
87
# File 'lib/sunspot/field.rb', line 85

def eql?(field)
  indexed_name == field.indexed_name
end

#hashObject

[View source]

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

def hash
  indexed_name.hash
end

#indexed_nameObject

Name with which this field is indexed internally. Based on public name and type.

Returns

String

Internal name of the field

[View source]

66
67
68
# File 'lib/sunspot/field.rb', line 66

def indexed_name
  @type.indexed_name(@name)
end

#multiple?Boolean

Whether this field accepts multiple values.

Returns

Boolean

True if this field accepts multiple values.

Returns:

  • (Boolean)
[View source]

77
78
79
# File 'lib/sunspot/field.rb', line 77

def multiple?
  !!@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

[View source]

32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sunspot/field.rb', line 32

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