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.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sunspot/field_factory.rb', line 50

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



66
67
68
# File 'lib/sunspot/field_factory.rb', line 66

def build
  @field
end

#populate_document(document, model, options = {}) ⇒ Object

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sunspot/field_factory.rb', line 74

def populate_document(document, model, options = {}) #:nodoc:
  atomic_operation = options[:update] == :set
  value = extract_value(model, options)
  if value != nil || atomic_operation
    indexed_values = Util.Array(@field.to_indexed(value))
    indexed_values = [nil] if indexed_values.empty? && atomic_operation
    indexed_values.each do |scalar_value|
      field_options = {}
      field_options[:boost] = @field.boost if @field.boost
      field_options[:null] = true if scalar_value.nil? && atomic_operation
      document.add_field(
        @field.indexed_name.to_sym,
        scalar_value,
        field_options.merge(options)
      )
    end
  end
end

#signatureObject

A unique signature identifying this field by name and type.



96
97
98
# File 'lib/sunspot/field_factory.rb', line 96

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