Class: Sunspot::Field::Base
- Inherits:
-
Object
- Object
- Sunspot::Field::Base
- Defined in:
- lib/sunspot/field.rb
Overview
Field classes encapsulate information about a field that has been configured for search and indexing. They expose methods that are useful for both operations.
Subclasses of Field::Base must implement the method #value_for
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
The public-facing name of the field.
-
#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.
-
#indexed_name ⇒ Object
The name of the field as it is indexed in Solr.
-
#initialize(name, type, options = {}) ⇒ Base
constructor
:nodoc.
-
#multiple? ⇒ Boolean
Returns.
-
#pair_for(model) ⇒ Object
A key-value pair where the key is the field’s indexed name and the value is the value that should be indexed for the given model.
-
#to_indexed(value) ⇒ Object
Convert a value to its representation for Solr indexing.
Constructor Details
#initialize(name, type, options = {}) ⇒ Base
:nodoc
14 15 16 17 18 |
# File 'lib/sunspot/field.rb', line 14 def initialize(name, type, = {}) #:nodoc @name, @type = name.to_sym, type @multiple = .delete(:multiple) raise ArgumentError, "Unknown field option #{options.keys.first.inspect} provided for field #{name.inspect}" unless .empty? end |
Instance Attribute Details
#name ⇒ Object
The public-facing name of the field
11 12 13 |
# File 'lib/sunspot/field.rb', line 11 def name @name end |
#type ⇒ Object
The Type of the field
12 13 14 |
# File 'lib/sunspot/field.rb', line 12 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
90 91 92 |
# File 'lib/sunspot/field.rb', line 90 def cast(value) type.cast(value) end |
#indexed_name ⇒ Object
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
48 49 50 |
# File 'lib/sunspot/field.rb', line 48 def indexed_name "#{type.indexed_name(name)}#{'m' if multiple?}" end |
#multiple? ⇒ Boolean
Returns
- Boolean
-
true if the field allows multiple values; false if not
97 98 99 |
# File 'lib/sunspot/field.rb', line 97 def multiple? !!@multiple end |
#pair_for(model) ⇒ Object
A key-value pair where the key is the field’s indexed name and the value is the value that should be indexed for the given model. This can be merged directly into the document hash for adding to solr-ruby.
Parameters
- model<Object>
-
the model from which to extract the value
Returns
- Hash
-
a single key-value pair with the field name and value
32 33 34 35 36 37 38 |
# File 'lib/sunspot/field.rb', line 32 def pair_for(model) unless (value = value_for(model)).nil? { indexed_name.to_sym => to_indexed(value) } else {} end 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
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/sunspot/field.rb', line 68 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 |