Class: Sunspot::Field
- Inherits:
-
Object
- Object
- Sunspot::Field
- Defined in:
- lib/sunspot/field.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Attribute Summary collapse
-
#boost ⇒ Object
readonly
Returns the value of attribute boost.
-
#name ⇒ Object
The public-facing name of the field.
-
#reference ⇒ Object
Model class that the value of this field refers to.
-
#type ⇒ Object
The Type of the field.
Instance Method Summary collapse
-
#cast(value) ⇒ Object
Cast the value into the appropriate Ruby class for the field’s type.
- #eql?(field) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#indexed_name ⇒ Object
Name with which this field is indexed internally.
-
#initialize(name, type, options = {}) ⇒ Field
constructor
A new instance of Field.
-
#more_like_this? ⇒ Boolean
Whether this field can be used for more_like_this queries.
-
#multiple? ⇒ Boolean
Whether this field accepts multiple values.
-
#to_indexed(value) ⇒ Object
Convert a value to its representation for Solr indexing.
Constructor Details
#initialize(name, type, options = {}) ⇒ Field
Returns a new instance of Field.
10 11 12 13 14 15 |
# File 'lib/sunspot/field.rb', line 10 def initialize(name, type, = {}) #:nodoc @name, @type = name.to_sym, type @stored = !!.delete(:stored) @more_like_this = !!.delete(:more_like_this) raise ArgumentError, "Field of type #{type} cannot be used for more_like_this" unless type.accepts_more_like_this? or !@more_like_this end |
Instance Attribute Details
#boost ⇒ Object (readonly)
Returns the value of attribute boost.
6 7 8 |
# File 'lib/sunspot/field.rb', line 6 def boost @boost end |
#name ⇒ Object
The public-facing name of the field
3 4 5 |
# File 'lib/sunspot/field.rb', line 3 def name @name end |
#reference ⇒ Object
Model class that the value of this field refers to
5 6 7 |
# File 'lib/sunspot/field.rb', line 5 def reference @reference end |
#type ⇒ Object
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
55 56 57 |
# File 'lib/sunspot/field.rb', line 55 def cast(value) @type.cast(value) end |
#eql?(field) ⇒ Boolean Also known as: ==
98 99 100 |
# File 'lib/sunspot/field.rb', line 98 def eql?(field) indexed_name == field.indexed_name end |
#hash ⇒ Object
94 95 96 |
# File 'lib/sunspot/field.rb', line 94 def hash indexed_name.hash end |
#indexed_name ⇒ Object
Name with which this field is indexed internally. Based on public name and type.
Returns
- String
-
Internal name of the field
67 68 69 |
# File 'lib/sunspot/field.rb', line 67 def indexed_name @type.indexed_name(@name) end |
#more_like_this? ⇒ Boolean
Whether this field can be used for more_like_this queries. If true, the field is configured to store termVectors.
Returns
- Boolean
-
True if this field can be used for more_like_this queries.
90 91 92 |
# File 'lib/sunspot/field.rb', line 90 def more_like_this? !!@more_like_this end |
#multiple? ⇒ Boolean
Whether this field accepts multiple values.
Returns
- Boolean
-
True if this field accepts multiple values.
78 79 80 |
# File 'lib/sunspot/field.rb', line 78 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
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sunspot/field.rb', line 33 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 |