Sha256: fd51b582209f9b09f28b194a7c09d9f79e107f9e94e8d47ae9e9b9e1710e1d18

Contents?: true

Size: 1.47 KB

Versions: 25

Compression:

Stored size: 1.47 KB

Contents

# encoding: utf-8
module Mongoid

  # This module contains behaviour 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] = send(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)
        self.shard_key_fields = names
      end
    end
  end
end

Version data entries

25 entries across 23 versions & 6 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-4.0.2/lib/mongoid/shardable.rb
mongoid-5.1.2 lib/mongoid/shardable.rb
mongoid-5.1.1 lib/mongoid/shardable.rb
mongoid-5.1.0 lib/mongoid/shardable.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.1/lib/mongoid/shardable.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/mongoid-5.0.2/lib/mongoid/shardable.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.2/lib/mongoid/shardable.rb
mongoid-5.0.2 lib/mongoid/shardable.rb
mongoid-5.0.1 lib/mongoid/shardable.rb
mongoid-5.0.0 lib/mongoid/shardable.rb
mongoid-5.0.0.rc0 lib/mongoid/shardable.rb
mongoid-5.0.0.beta lib/mongoid/shardable.rb
mongoid-4.0.2 lib/mongoid/shardable.rb
mongoid-4.0.1 lib/mongoid/shardable.rb
mongoid-4.0.0 lib/mongoid/shardable.rb
mongoid-4.0.0.rc2 lib/mongoid/shardable.rb
mongoid-4.0.0.rc1 lib/mongoid/shardable.rb
mongoid-4.0.0.beta2 lib/mongoid/shardable.rb
mongoid-4.0.0.beta1 lib/mongoid/shardable.rb
mongoid-4.0.0.alpha2 lib/mongoid/shardable.rb