Module: Mongoid::Shardable
Overview
This module contains behavior for adding shard key fields to updates.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#shard_key_field_value(field, prefer_persisted:) ⇒ Object
private
Returns the value for the named shard key.
-
#shard_key_fields ⇒ Array<String>
Get the shard key fields.
-
#shard_key_selector(prefer_persisted: false) ⇒ Hash
private
Returns the selector that would match the defined shard keys.
-
#shard_key_selector_in_db ⇒ Hash
private
Returns the selector that would match the existing version of this document in the database.
Instance Method Details
#shard_key_field_value(field, prefer_persisted:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the value for the named shard key. If the field identifies
an embedded document, the key will be parsed and recursively evaluated.
If prefer_persisted is true, the value last persisted to the database
will be returned, regardless of what the current value of the attribute
may be.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mongoid/shardable.rb', line 94 def shard_key_field_value(field, prefer_persisted:) if field.include?('.') relation, remaining = field.split('.', 2) send(relation)&.shard_key_field_value(remaining, prefer_persisted: prefer_persisted) elsif prefer_persisted && !new_record? attribute_was(field) else send(field) end end |
#shard_key_fields ⇒ Array<String>
Refactored from using delegate for class load performance.
Get the shard key fields.
45 46 47 |
# File 'lib/mongoid/shardable.rb', line 45 def shard_key_fields self.class.shard_key_fields end |
#shard_key_selector(prefer_persisted: false) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the selector that would match the defined shard keys. If
prefer_persisted is false (the default), it uses the current values
of the specified shard keys, otherwise, it will try to use whatever value
was most recently persisted.
61 62 63 64 65 |
# File 'lib/mongoid/shardable.rb', line 61 def shard_key_selector(prefer_persisted: false) shard_key_fields.each_with_object({}) do |field, selector| selector[field.to_s] = shard_key_field_value(field.to_s, prefer_persisted: prefer_persisted) end end |
#shard_key_selector_in_db ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the selector that would match the existing version of this document in the database.
If the document is not persisted, this method uses the current values of the shard key fields. If the document is persisted, this method uses the values retrieved from the database.
77 78 79 |
# File 'lib/mongoid/shardable.rb', line 77 def shard_key_selector_in_db shard_key_selector(prefer_persisted: true) end |