module Mongoid::Shardable
This module contains behavior for adding shard key fields to updates.
Public Instance Methods
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.
@param [String] field The name of the field to evaluate @param [ true|false ] prefer_persisted Whether or not to prefer the
persisted value over the current value.
@return [ Object
] The value of the named field.
@api private
# 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
Get the shard key fields.
@note Refactored from using delegate for class load performance.
@example Get the shard key fields.
model.shard_key_fields
@return [ Array<String> ] The shard key field names.
# File lib/mongoid/shardable.rb, line 47 def shard_key_fields self.class.shard_key_fields end
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.
@param [ true | false ] prefer_persisted Whether to use the current
value of the shard key fields, or to use their most recently persisted values.
@return [ Hash ] The shard key selector.
@api private
# 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
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.
@return [ Hash ] The shard key selector.
@api private
# File lib/mongoid/shardable.rb, line 79 def shard_key_selector_in_db shard_key_selector(prefer_persisted: true) end