Sha256: 9589737ddd3a8c1a66a74b1d4b278b3db1fba2f7fa29f1194f497575d598032b

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid

  # This module contains behavior for adding shard key fields to updates.
  #
  # @since 4.0.0
  module Shardable
    extend ActiveSupport::Concern

    included do
      cattr_accessor :shard_key_fields
      self.shard_key_fields = []
    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.
    #
    # @since 1.0.0
    def shard_key_fields
      self.class.shard_key_fields
    end

    # Get the document selector with the defined shard keys.
    #
    # @example Get the selector for the shard keys.
    #   person.shard_key_selector
    #
    # @return [ Hash ] The shard key selector.
    #
    # @since 2.0.0
    def shard_key_selector
      selector = {}
      shard_key_fields.each do |field|
        selector[field.to_s] = new_record? ? send(field) : attribute_was(field)
      end
      selector
    end

    module ClassMethods

      # Specifies a shard key with the field(s) specified.
      #
      # @example Specify the shard key.
      #
      #   class Person
      #     include Mongoid::Document
      #     field :first_name, :type => String
      #     field :last_name, :type => String
      #
      #     shard_key :first_name, :last_name
      #   end
      #
      # @since 2.0.0
      def shard_key(*names)
        names.each do |name|
          self.shard_key_fields << self.database_field_name(name).to_sym
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-7.1.0.rc0 lib/mongoid/shardable.rb