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.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mongoid/shardable.rb', line 96 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.
47 48 49 |
# File 'lib/mongoid/shardable.rb', line 47 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.
63 64 65 66 67 |
# File 'lib/mongoid/shardable.rb', line 63 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.
79 80 81 |
# File 'lib/mongoid/shardable.rb', line 79 def shard_key_selector_in_db shard_key_selector(prefer_persisted: true) end |