Class: Sunspot::Field::StaticField
- Inherits:
-
Object
- Object
- Sunspot::Field::StaticField
- Extended by:
- Buildable
- Includes:
- FieldInstance
- 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
Instance Attribute Summary collapse
-
#name ⇒ Object
The public-facing name of the field.
-
#type ⇒ Object
The Type of the field.
Instance Method Summary collapse
-
#initialize(name, type, data_extractor, options = {}) ⇒ StaticField
constructor
:nodoc.
-
#pairs_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.
Methods included from Buildable
Methods included from FieldInstance
#cast, #indexed_name, #to_indexed
Constructor Details
#initialize(name, type, data_extractor, options = {}) ⇒ StaticField
:nodoc
118 119 120 121 122 123 124 125 |
# File 'lib/sunspot/field.rb', line 118 def initialize(name, type, data_extractor, = {}) #:nodoc unless name.to_s =~ /^\w+$/ raise ArgumentError, "Invalid field name #{name}: only letters, numbers, and underscores are allowed." end @name, @type, @data_extractor = name.to_sym, type, data_extractor @multiple = !!.delete(:multiple) raise ArgumentError, "Unknown field option #{.keys.first.inspect} provided for field #{name.inspect}" unless .empty? end |
Instance Attribute Details
#name ⇒ Object
The public-facing name of the field
115 116 117 |
# File 'lib/sunspot/field.rb', line 115 def name @name end |
#type ⇒ Object
The Type of the field
116 117 118 |
# File 'lib/sunspot/field.rb', line 116 def type @type end |
Instance Method Details
#pairs_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
139 140 141 142 143 144 145 |
# File 'lib/sunspot/field.rb', line 139 def pairs_for(model) unless (value = @data_extractor.value_for(model)).nil? { indexed_name.to_sym => to_indexed(value) } else {} end end |