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.



83
84
85
86
# File 'lib/sunspot/field_factory.rb', line 83

def initialize(name, type, options = {}, &block)
  super(name, options, &block)
  @type, @options = type, options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



81
82
83
# File 'lib/sunspot/field_factory.rb', line 81

def name
  @name
end

#typeObject

Returns the value of attribute type.



81
82
83
# File 'lib/sunspot/field_factory.rb', line 81

def type
  @type
end

Instance Method Details

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

Build a field based on the dynamic name given.



91
92
93
# File 'lib/sunspot/field_factory.rb', line 91

def build(dynamic_name)
  AttributeField.new("#{@name}:#{dynamic_name}", @type, @options.dup)
end

#populate_document(document, model) ⇒ Object

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



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sunspot/field_factory.rb', line 104

def populate_document(document, model)
  if values = @data_extractor.value_for(model)
    values.each_pair do |dynamic_name, value|
      field_instance = build(dynamic_name)
      for scalar_value in Array(field_instance.to_indexed(value))
        document.add_field(
          field_instance.indexed_name.to_sym,
          scalar_value
        )
      end
    end
  end
end

#signatureObject

Unique signature identifying this dynamic field based on name and type



121
122
123
# File 'lib/sunspot/field_factory.rb', line 121

def signature
  [@name, @type]
end