Class: Sunspot::FieldFactory::Static

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

Overview

A StaticFieldFactory generates normal static fields. Each factory instance contains an eager-initialized field instance, which is returned by the #build method.

Instance Attribute Summary

Attributes inherited from Abstract

#name

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}, &block) ⇒ Static

Returns a new instance of Static.



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

def initialize(name, type, options = {}, &block)
  super(name, options, &block)
  unless name.to_s =~ /^\w+$/
    raise ArgumentError, "Invalid field name #{name}: only letters, numbers, and underscores are allowed."
  end
  @field =
    if type.is_a?(Type::TextType)
      FulltextField.new(name, options)
    else
      AttributeField.new(name, type, options)
    end
end

Instance Method Details

#buildObject

Return the field instance built by this factory



49
50
51
# File 'lib/sunspot/field_factory.rb', line 49

def build
  @field
end

#populate_document(document, model) ⇒ Object

Extract the encapsulated field’s data from the given model and add it into the Solr document for indexing.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sunspot/field_factory.rb', line 57

def populate_document(document, model) #:nodoc:
  unless (value = @data_extractor.value_for(model)).nil?
    Util.Array(@field.to_indexed(value)).each do |scalar_value|
      options = {}
      options[:boost] = @field.boost if @field.boost
      document.add_field(
        @field.indexed_name.to_sym,
        scalar_value,
        options
      )
    end
  end
end

#signatureObject

A unique signature identifying this field by name and type.



74
75
76
# File 'lib/sunspot/field_factory.rb', line 74

def signature
  [@field.name, @field.type]
end