Class: Sunspot::Field::StaticField

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Buildable

build

Methods included from FieldInstance

#cast, #indexed_name, #to_indexed

Constructor Details

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

:nodoc

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
125
# File 'lib/sunspot/field.rb', line 118

def initialize(name, type, data_extractor, options = {}) #: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 = !!options.delete(:multiple)
  raise ArgumentError, "Unknown field option #{options.keys.first.inspect} provided for field #{name.inspect}" unless options.empty?
end

Instance Attribute Details

#nameObject

The public-facing name of the field



115
116
117
# File 'lib/sunspot/field.rb', line 115

def name
  @name
end

#typeObject

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