Class: Sunspot::Setup
- Inherits:
-
Object
- Object
- Sunspot::Setup
- Defined in:
- lib/sunspot/setup.rb
Overview
This class encapsulates the search/indexing setup for a given class. Its contents are built using the Sunspot.setup method.
Instance Attribute Summary collapse
-
#class_object_id ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
.for(clazz) ⇒ Object
Retrieve the setup instance for the given class, or for the nearest ancestor that has a setup, if any.
-
.setup(clazz, &block) ⇒ Object
Retrieve or create the Setup instance for the given class, evaluating the given block to add to the setup’s configuration.
Instance Method Summary collapse
-
#add_document_boost(attr_name, &block) ⇒ Object
Add a document boost to documents at index time.
-
#add_dynamic_field_factory(name, type, options = {}, &block) ⇒ Object
Add dynamic field_factories.
-
#add_field_factory(name, type, options = {}, &block) ⇒ Object
Add field factory for scope/ordering.
-
#add_id_prefix(attr_name, &block) ⇒ Object
Add id prefix for compositeId router.
- #add_join_field_factory(name, type, options = {}, &block) ⇒ Object
-
#add_text_field_factory(name, options = {}, &block) ⇒ Object
Add field_factories for fulltext search.
-
#all_field_factories ⇒ Object
Get all static, dynamic, and text field_factories associated with this setup as well as all inherited field_factories.
-
#all_more_like_this_fields ⇒ Object
Return all more_like_this fields.
-
#all_text_fields ⇒ Object
Return all text fields.
-
#clazz ⇒ Object
Return the class associated with this setup.
-
#document_boost_for(model) ⇒ Object
Get the document boost for a given model.
-
#dynamic_field_factories ⇒ Object
Get all dynamic field_factories for this and parent setups.
-
#dynamic_field_factory(field_name) ⇒ Object
Return the DynamicFieldFactory with the given base name.
-
#field(field_name) ⇒ Object
Return the Field with the given (public-facing) name.
-
#field_factories ⇒ Object
Get the field_factories associated with this setup as well as all inherited field_factories.
-
#fields ⇒ Object
Return all attribute fields.
-
#id_prefix_defined? ⇒ Boolean
Check if ‘id_prefix` is defined for class associated with this setup.
- #id_prefix_for(model) ⇒ Object
-
#id_prefix_for_class ⇒ Object
Get value for ‘id_prefix` defined as String.
-
#id_prefix_requires_instance? ⇒ Boolean
Check if instance is required to get ‘id_prefix` value (instance is required for Proc and Symbol `id_prefix` only. Value for String `id_prefix` can be get on class level).
-
#initialize(clazz) ⇒ Setup
constructor
A new instance of Setup.
-
#more_like_this_fields(field_name) ⇒ Object
Return one or more more_like_this fields (can be either attribute or text fields) for the given name.
-
#setup(&block) ⇒ Object
Builder method for evaluating the setup DSL.
-
#stored_fields(field_name, dynamic_field_name = nil) ⇒ Object
Return one or more stored fields (can be either attribute or text fields) for the given name.
-
#text_field_factories ⇒ Object
Get the text field_factories associated with this setup as well as all inherited text field_factories.
-
#text_fields(field_name) ⇒ Object
Return one or more text fields with the given public-facing name.
- #type_names ⇒ Object
Constructor Details
#initialize(clazz) ⇒ Setup
Returns a new instance of Setup.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/sunspot/setup.rb', line 8 def initialize(clazz) @class_object_id = clazz.object_id @class_name = clazz.name @field_factories, @text_field_factories, @dynamic_field_factories, @field_factories_cache, @text_field_factories_cache, @dynamic_field_factories_cache = *Array.new(6) { Hash.new } @stored_field_factories_cache = Hash.new { |h, k| h[k] = [] } @more_like_this_field_factories_cache = Hash.new { |h, k| h[k] = [] } @dsl = DSL::Fields.new(self) @document_boost_extractor = nil @id_prefix_extractor = nil add_field_factory(:class, Type::ClassType.instance) end |
Instance Attribute Details
#class_object_id ⇒ Object (readonly)
:nodoc:
7 8 9 |
# File 'lib/sunspot/setup.rb', line 7 def class_object_id @class_object_id end |
Class Method Details
.for(clazz) ⇒ Object
Retrieve the setup instance for the given class, or for the nearest ancestor that has a setup, if any.
Parameters
- clazz<Class>
-
Class for which to retrieve a setup
Returns
- Sunspot::Setup
-
Setup instance associated with the given class or its nearest ancestor
392 393 394 |
# File 'lib/sunspot/setup.rb', line 392 def for(clazz) #:nodoc: setups[clazz.name.to_sym] || self.for(clazz.superclass) if clazz end |
.setup(clazz, &block) ⇒ Object
Retrieve or create the Setup instance for the given class, evaluating the given block to add to the setup’s configuration
375 376 377 |
# File 'lib/sunspot/setup.rb', line 375 def setup(clazz, &block) #:nodoc: self.for!(clazz).setup(&block) end |
Instance Method Details
#add_document_boost(attr_name, &block) ⇒ Object
Add a document boost to documents at index time. Document boost can be static (the same for all documents of this class), or extracted on a per- document basis using either attribute or block extraction as per usual.
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sunspot/setup.rb', line 100 def add_document_boost(attr_name, &block) @document_boost_extractor = if attr_name if attr_name.respond_to?(:to_f) DataExtractor::Constant.new(attr_name) else DataExtractor::AttributeExtractor.new(attr_name) end else DataExtractor::BlockExtractor.new(&block) end end |
#add_dynamic_field_factory(name, type, options = {}, &block) ⇒ Object
Add dynamic field_factories
Parameters
- field_factories<Array>
-
Array of dynamic field objects
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sunspot/setup.rb', line 81 def add_dynamic_field_factory(name, type, = {}, &block) stored, more_like_this = [:stored], [:more_like_this] field_factory = FieldFactory::Dynamic.new(name, type, , &block) @dynamic_field_factories[field_factory.signature] = field_factory @dynamic_field_factories_cache[field_factory.name] = field_factory @field_factories_cache[field_factory.name] = field_factory if stored @stored_field_factories_cache[field_factory.name] << field_factory end if more_like_this @more_like_this_field_factories_cache[field_factory.name] << field_factory end end |
#add_field_factory(name, type, options = {}, &block) ⇒ Object
Add field factory for scope/ordering
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sunspot/setup.rb', line 29 def add_field_factory(name, type, = {}, &block) stored, more_like_this = [:stored], [:more_like_this] field_factory = FieldFactory::Static.new(name, type, , &block) @field_factories[field_factory.signature] = field_factory @field_factories_cache[field_factory.name] = field_factory if stored @stored_field_factories_cache[field_factory.name] << field_factory end if more_like_this @more_like_this_field_factories_cache[field_factory.name] << field_factory end end |
#add_id_prefix(attr_name, &block) ⇒ Object
Add id prefix for compositeId router
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sunspot/setup.rb', line 116 def add_id_prefix(attr_name, &block) @id_prefix_extractor = case attr_name when Symbol DataExtractor::AttributeExtractor.new(attr_name) when String DataExtractor::Constant.new(attr_name) when nil DataExtractor::BlockExtractor.new(&block) if block_given? else raise ArgumentError, "The ID prefix has to be either a Symbol, a String or a Proc" end end |
#add_join_field_factory(name, type, options = {}, &block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sunspot/setup.rb', line 42 def add_join_field_factory(name, type, = {}, &block) field_factory = FieldFactory::Join.new(name, type, , &block) @field_factories[field_factory.signature] = field_factory if type.is_a?(Type::TextType) @text_field_factories_cache[field_factory.name] = field_factory else @field_factories_cache[field_factory.name] = field_factory end end |
#add_text_field_factory(name, options = {}, &block) ⇒ Object
Add field_factories for fulltext search
Parameters
- field_factories<Array>
-
Array of Sunspot::Field objects
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/sunspot/setup.rb', line 60 def add_text_field_factory(name, = {}, &block) stored, more_like_this = [:stored], [:more_like_this] field_factory = FieldFactory::Static.new(name, Type::TextType.instance, , &block) @text_field_factories[name] = field_factory @text_field_factories_cache[field_factory.name] = field_factory @field_factories_cache[field_factory.name] = field_factory if stored @stored_field_factories_cache[field_factory.name] << field_factory end if more_like_this @more_like_this_field_factories_cache[field_factory.name] << field_factory end end |
#all_field_factories ⇒ Object
Get all static, dynamic, and text field_factories associated with this setup as well as all inherited field_factories
Returns
- Array
-
Collection of all text and scope field_factories associated with this setup
258 259 260 261 262 |
# File 'lib/sunspot/setup.rb', line 258 def all_field_factories all_field_factories = [] all_field_factories.concat(field_factories).concat(text_field_factories).concat(dynamic_field_factories) all_field_factories end |
#all_more_like_this_fields ⇒ Object
Return all more_like_this fields
221 222 223 224 225 |
# File 'lib/sunspot/setup.rb', line 221 def all_more_like_this_fields @more_like_this_field_factories_cache.values.map do |field_factories| field_factories.map { |field_factory| field_factory.build } end.flatten end |
#all_text_fields ⇒ Object
Return all text fields
214 215 216 |
# File 'lib/sunspot/setup.rb', line 214 def all_text_fields text_field_factories.map { |text_field_factory| text_field_factory.build } end |
#clazz ⇒ Object
Return the class associated with this setup.
Returns
- clazz<Class>
-
Class setup is configured for
282 283 284 |
# File 'lib/sunspot/setup.rb', line 282 def clazz Util.full_const_get(@class_name) end |
#document_boost_for(model) ⇒ Object
Get the document boost for a given model
289 290 291 292 293 |
# File 'lib/sunspot/setup.rb', line 289 def document_boost_for(model) if @document_boost_extractor @document_boost_extractor.value_for(model) end end |
#dynamic_field_factories ⇒ Object
Get all dynamic field_factories for this and parent setups
Returns
- Array
-
Dynamic field_factories
271 272 273 |
# File 'lib/sunspot/setup.rb', line 271 def dynamic_field_factories collection_from_inheritable_hash(:dynamic_field_factories) end |
#dynamic_field_factory(field_name) ⇒ Object
Return the DynamicFieldFactory with the given base name
197 198 199 200 201 202 |
# File 'lib/sunspot/setup.rb', line 197 def dynamic_field_factory(field_name) @dynamic_field_factories_cache[field_name.to_sym] || raise( UnrecognizedFieldError, "No dynamic field configured for #{@class_name} with name '#{field_name}'" ) end |
#field(field_name) ⇒ Object
Return the Field with the given (public-facing) name
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/sunspot/setup.rb', line 141 def field(field_name) if field_factory = @field_factories_cache[field_name.to_sym] field_factory.build else raise( UnrecognizedFieldError, "No field configured for #{@class_name} with name '#{field_name}'" ) end end |
#field_factories ⇒ Object
Get the field_factories associated with this setup as well as all inherited field_factories
Returns
- Array
-
Collection of all field_factories associated with this setup
234 235 236 |
# File 'lib/sunspot/setup.rb', line 234 def field_factories collection_from_inheritable_hash(:field_factories) end |
#fields ⇒ Object
Return all attribute fields
207 208 209 |
# File 'lib/sunspot/setup.rb', line 207 def fields field_factories.map { |field_factory| field_factory.build } end |
#id_prefix_defined? ⇒ Boolean
Check if ‘id_prefix` is defined for class associated with this setup.
Returns
- Boolean
-
True if class associated with this setup has defined ‘id_prefix`
325 326 327 |
# File 'lib/sunspot/setup.rb', line 325 def id_prefix_defined? !@id_prefix_extractor.nil? end |
#id_prefix_for(model) ⇒ Object
295 296 297 298 299 300 301 302 303 |
# File 'lib/sunspot/setup.rb', line 295 def id_prefix_for(model) if @id_prefix_extractor value = @id_prefix_extractor.value_for(model) if value.is_a?(String) and value.size > 0 value[-1] == "!" ? value : "#{value}!" end end end |
#id_prefix_for_class ⇒ Object
Get value for ‘id_prefix` defined as String
Returns
- String
-
value for ‘id_prefix` defined as String
312 313 314 315 316 |
# File 'lib/sunspot/setup.rb', line 312 def id_prefix_for_class return if !id_prefix_defined? || id_prefix_requires_instance? @id_prefix_extractor.value_for(nil) end |
#id_prefix_requires_instance? ⇒ Boolean
Check if instance is required to get ‘id_prefix` value (instance is required for Proc and Symbol `id_prefix` only. Value for String `id_prefix` can be get on class level)
Returns
- Boolean
-
True if instance is required to get ‘id_prefix` value
337 338 339 340 341 |
# File 'lib/sunspot/setup.rb', line 337 def id_prefix_requires_instance? return false unless id_prefix_defined? !@id_prefix_extractor.is_a?(DataExtractor::Constant) end |
#more_like_this_fields(field_name) ⇒ Object
Return one or more more_like_this fields (can be either attribute or text fields) for the given name.
188 189 190 191 192 |
# File 'lib/sunspot/setup.rb', line 188 def more_like_this_fields(field_name) @more_like_this_field_factories_cache[field_name.to_sym].map do |field_factory| field_factory.build end end |
#setup(&block) ⇒ Object
Builder method for evaluating the setup DSL
134 135 136 |
# File 'lib/sunspot/setup.rb', line 134 def setup(&block) Util.instance_eval_or_call(@dsl, &block) end |
#stored_fields(field_name, dynamic_field_name = nil) ⇒ Object
Return one or more stored fields (can be either attribute or text fields) for the given name.
174 175 176 177 178 179 180 181 182 |
# File 'lib/sunspot/setup.rb', line 174 def stored_fields(field_name, dynamic_field_name = nil) @stored_field_factories_cache[field_name.to_sym].map do |field_factory| if dynamic_field_name field_factory.build(dynamic_field_name) else field_factory.build end end end |
#text_field_factories ⇒ Object
Get the text field_factories associated with this setup as well as all inherited text field_factories
Returns
- Array
-
Collection of all text field_factories associated with this setup
246 247 248 |
# File 'lib/sunspot/setup.rb', line 246 def text_field_factories collection_from_inheritable_hash(:text_field_factories) end |
#text_fields(field_name) ⇒ Object
Return one or more text fields with the given public-facing name. This implementation will always return a single field (in an array), but CompositeSetup objects might return more than one.
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/sunspot/setup.rb', line 157 def text_fields(field_name) text_field = if field_factory = @text_field_factories_cache[field_name.to_sym] field_factory.build else raise( UnrecognizedFieldError, "No text field configured for #{@class_name} with name '#{field_name}'" ) end [text_field] end |
#type_names ⇒ Object
22 23 24 |
# File 'lib/sunspot/setup.rb', line 22 def type_names [@class_name] end |