Class: Sunspot::FieldFactory::Dynamic

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

Overview

DynamicFieldFactories create dynamic field instances based on dynamic configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Dynamic.



132
133
134
135
136
# File 'lib/sunspot/field_factory.rb', line 132

def initialize(name, type, options = {}, &block)
  super(name, options, &block)
  @type, @options = type, options
  @separator = @options.delete(:separator) || ':'
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



130
131
132
# File 'lib/sunspot/field_factory.rb', line 130

def name
  @name
end

#separatorObject

Returns the value of attribute separator.



130
131
132
# File 'lib/sunspot/field_factory.rb', line 130

def separator
  @separator
end

#typeObject

Returns the value of attribute type.



130
131
132
# File 'lib/sunspot/field_factory.rb', line 130

def type
  @type
end

Instance Method Details

#build(dynamic_name) ⇒ Object Also known as: field

Build a field based on the dynamic name given.



141
142
143
# File 'lib/sunspot/field_factory.rb', line 141

def build(dynamic_name)
  AttributeField.new([@name, dynamic_name].join(separator), @type, @options.dup)
end

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

Generate dynamic fields based on hash returned by data accessor and add the field data to the document.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sunspot/field_factory.rb', line 154

def populate_document(document, model, options = {})
  values = extract_value(model, options)
  if values
    values.each_pair do |dynamic_name, value|
      field_instance = build(dynamic_name)
      Util.Array(field_instance.to_indexed(value)).each do |scalar_value|
        document.add_field(
          field_instance.indexed_name.to_sym,
          scalar_value,
          options
        )
      end
    end
  end
end

#signatureObject

Unique signature identifying this dynamic field based on name and type



173
174
175
# File 'lib/sunspot/field_factory.rb', line 173

def signature
  [@name, @type]
end