Module: Mongoid::Fields::ClassMethods
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Extended In:
| |
| Defined in: | lib/mongoid/fields.rb, lib/mongoid/fields.rb |
Instance Attribute Summary
-
#using_object_ids? ⇒ true | false
readonly
Convenience method for determining if we are using
BSON::ObjectIdsas our id.
Instance Method Summary
-
#attribute_names ⇒ Array<String>
Returns an array of names for the attributes available on this object.
-
#auto_embed_field(name, model: 'voyage-4', num_dimensions: nil, quantization: nil, similarity: nil, index: nil)
Declares a text field and registers a corresponding Atlas Vector Search index for it using the auto-embedding (autoEmbed) type.
-
#cleanse_localized_field_names(name) ⇒ Field
Removes the _translations from the given field name.
-
#database_field_name(name) ⇒ String
Get the name of the provided field as it is stored in the database.
-
#extract_id_field(attributes) ⇒ Object
Internal use only
Internal use only
Extracts the id field from the specified attributes hash based on aliases defined in this class.
-
#field(name, options = {}) ⇒ Field
Defines all the fields that are accessible on the
::Mongoid::DocumentFor each field that is defined, a getter and setter will be added as an instance method to the::Mongoid::Document. -
#id_fields ⇒ Array<Symbol | String>
Internal use only
Internal use only
Returns the list of id fields for this model class, as both strings and symbols.
-
#replace_field(name, type) ⇒ Serializable
Replace a field with a new type.
-
#traverse_association_tree(key) {|The, The, Whether| ... } ⇒ Field
Internal use only
Internal use only
Traverse down the association tree and search for the field for the given key.
-
#vector_field(name, dimensions:, similarity: 'cosine', index: nil)
Declares a vector embedding field and registers a corresponding Atlas Vector Search index for it in one step.
Instance Attribute Details
#using_object_ids? ⇒ true | false (readonly)
Convenience method for determining if we are using BSON::ObjectIds as
our id.
# File 'lib/mongoid/fields.rb', line 608
def using_object_ids? fields['_id'].object_id_field? end
Instance Method Details
#attribute_names ⇒ Array<String>
Returns an array of names for the attributes available on this object.
Provides the field names in an ORM-agnostic way. ::Rails v3.1+ uses this
method to automatically wrap params in JSON requests.
# File 'lib/mongoid/fields.rb', line 449
def attribute_names fields.keys end
#auto_embed_field(name, model: 'voyage-4', num_dimensions: nil, quantization: nil, similarity: nil, index: nil)
Declares a text field and registers a corresponding Atlas Vector Search index for it using the auto-embedding (autoEmbed) type. Atlas generates the embeddings automatically at index and query time; no pre-computed vectors are required.
# File 'lib/mongoid/fields.rb', line 542
def (name, model: 'voyage-4', num_dimensions: nil, quantization: nil, similarity: nil, index: nil) field(name, type: String) field_spec = { type: 'autoEmbed', modality: 'text', path: name.to_s, model: model } field_spec[:numDimensions] = num_dimensions if num_dimensions field_spec[:quantization] = quantization if quantization field_spec[:similarity] = similarity if similarity if index vector_search_index(index, fields: [ field_spec ]) else vector_search_index(fields: [ field_spec ]) end end
#cleanse_localized_field_names(name) ⇒ Field
Removes the _translations from the given field name. This is done only when there doesn't already exist a field name or relation with the same name (i.e. with the _translations suffix). This check for an existing field is done recursively
# File 'lib/mongoid/fields.rb', line 98
def cleanse_localized_field_names(name) name = database_field_name(name.to_s) klass = self [].tap do |res| ar = name.split('.') ar.each_with_index do |fn, i| key = fn unless klass.fields.key?(fn) || klass.relations.key?(fn) key = if fn.end_with?(TRANSLATIONS_SFX) fn.delete_suffix(TRANSLATIONS_SFX) else fn end end res.push(key) if klass.fields.key?(fn) res.push(ar.drop(i + 1).join('.')) unless i == ar.length - 1 break elsif klass.relations.key?(fn) klass = klass.relations[key].klass end end end.join('.') end
#database_field_name(name) ⇒ String
Get the name of the provided field as it is stored in the database. Used in determining if the field is aliased or not.
# File 'lib/mongoid/fields.rb', line 459
def database_field_name(name) Fields.database_field_name(name, relations, aliased_fields, aliased_associations) end
#extract_id_field(attributes) ⇒ Object
Extracts the id field from the specified attributes hash based on aliases defined in this class.
# File 'lib/mongoid/fields.rb', line 81
def extract_id_field(attributes) id_fields.each do |k| if v = attributes[k] return v end end nil end
#field(name, options = {}) ⇒ Field
Defines all the fields that are accessible on the ::Mongoid::Document
For each field that is defined, a getter and setter will be
added as an instance method to the ::Mongoid::Document.
# File 'lib/mongoid/fields.rb', line 577
def field(name, = {}) named = name.to_s Validators::Macro.validate(self, name, ) added = add_field(named, ) descendants.each do |subclass| subclass.add_field(named, ) end added end
#id_fields ⇒ Array<Symbol | String>
Returns the list of id fields for this model class, as both strings and symbols.
# File 'lib/mongoid/fields.rb', line 62
def id_fields IDS.dup.tap do |id_fields| aliased_fields.each do |k, v| if v == '_id' id_fields << k.to_sym id_fields << k end end end end
#replace_field(name, type) ⇒ Serializable
Replace a field with a new type.
# File 'lib/mongoid/fields.rb', line 596
def replace_field(name, type) remove_defaults(name) add_field(name, fields[name]..merge(type: type)) end
#traverse_association_tree(key) {|The, The, Whether| ... } ⇒ Field
Traverse down the association tree and search for the field for the given key.
# File 'lib/mongoid/fields.rb', line 627
def traverse_association_tree(key, &block) Fields.traverse_association_tree(key, fields, relations, aliased_associations, &block) end
#vector_field(name, dimensions:, similarity: 'cosine', index: nil)
# File 'lib/mongoid/fields.rb', line 490
def vector_field(name, dimensions:, similarity: 'cosine', index: nil) field(name, type: Array) field_spec = { type: 'vector', path: name.to_s, numDimensions: dimensions, similarity: similarity } if index vector_search_index(index, fields: [ field_spec ]) else vector_search_index(fields: [ field_spec ]) end end