Class: Sunspot::FieldFactory::Dynamic
- Defined in:
- lib/sunspot/field_factory.rb
Overview
DynamicFieldFactories create dynamic field instances based on dynamic configuration.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#build(dynamic_name) ⇒ Object
(also: #field)
Build a field based on the dynamic name given.
-
#initialize(name, type, options = {}, &block) ⇒ Dynamic
constructor
A new instance of Dynamic.
-
#populate_document(document, model) ⇒ Object
Generate dynamic fields based on hash returned by data accessor and add the field data to the document.
-
#signature ⇒ Object
Unique signature identifying this dynamic field based on name and type.
Constructor Details
#initialize(name, type, options = {}, &block) ⇒ Dynamic
Returns a new instance of Dynamic.
119 120 121 122 |
# File 'lib/sunspot/field_factory.rb', line 119 def initialize(name, type, = {}, &block) super(name, , &block) @type, @options = type, end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
117 118 119 |
# File 'lib/sunspot/field_factory.rb', line 117 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
117 118 119 |
# File 'lib/sunspot/field_factory.rb', line 117 def type @type end |
Instance Method Details
#build(dynamic_name) ⇒ Object Also known as: field
Build a field based on the dynamic name given.
127 128 129 |
# File 'lib/sunspot/field_factory.rb', line 127 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.
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/sunspot/field_factory.rb', line 140 def populate_document(document, model) if values = @data_extractor.value_for(model) 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 ) end end end end |
#signature ⇒ Object
Unique signature identifying this dynamic field based on name and type
157 158 159 |
# File 'lib/sunspot/field_factory.rb', line 157 def signature [@name, @type] end |