Sha256: 366f85c3f7a16dda5033e9b7baf9b85d9097e77819a7b2e6aec593fd69096e75

Contents?: true

Size: 1.79 KB

Versions: 27

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid
  module Clients
    module Validators

      # Validates the options passed to :store_in.
      module Storage
        extend self

        # The valid options for storage.
        #
        # @since 3.0.0
        VALID_OPTIONS = [ :collection, :database, :client ].freeze

        # Validate the options provided to :store_in.
        #
        # @example Validate the options.
        #   Storage.validate(:collection_name)
        #
        # @param [ Class ] klass The model class.
        # @param [ Hash, String, Symbol ] options The provided options.
        #
        # @since 3.0.0
        def validate(klass, options)
          valid_keys?(options) or raise Errors::InvalidStorageOptions.new(klass, options)
          valid_parent?(klass) or raise Errors::InvalidStorageParent.new(klass)
        end

        private
        # Determine if the current klass is valid to change store_in
        # options
        #
        # @api private
        #
        # @param [ Class ] klass
        #
        # @return [ true, false ] If the class is valid
        #
        # @since 4.0.0
        def valid_parent?(klass)
          !klass.superclass.include?(Mongoid::Document)
        end

        # Determine if all keys in the options hash are valid.
        #
        # @api private
        #
        # @example Are all keys valid?
        #   validator.valid_keys?({ collection: "name" })
        #
        # @param [ Hash ] options The options hash.
        #
        # @return [ true, false ] If all keys are valid.
        #
        # @since 3.0.0
        def valid_keys?(options)
          return false unless options.is_a?(::Hash)
          options.keys.all? do |key|
            VALID_OPTIONS.include?(key)
          end
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
mongoid-7.3.5 lib/mongoid/clients/validators/storage.rb
mongoid-7.3.4 lib/mongoid/clients/validators/storage.rb
mongoid-7.1.11 lib/mongoid/clients/validators/storage.rb
mongoid-7.2.6 lib/mongoid/clients/validators/storage.rb
mongoid-7.3.3 lib/mongoid/clients/validators/storage.rb
mongoid-7.3.2 lib/mongoid/clients/validators/storage.rb
mongoid-7.2.5 lib/mongoid/clients/validators/storage.rb
mongoid-7.1.10 lib/mongoid/clients/validators/storage.rb
mongoid-7.1.9 lib/mongoid/clients/validators/storage.rb
mongoid-7.2.4 lib/mongoid/clients/validators/storage.rb
mongoid-7.3.1 lib/mongoid/clients/validators/storage.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/clients/validators/storage.rb
mongoid-7.3.0 lib/mongoid/clients/validators/storage.rb
mongoid-7.2.3 lib/mongoid/clients/validators/storage.rb
mongoid-7.1.8 lib/mongoid/clients/validators/storage.rb
mongoid-7.2.2 lib/mongoid/clients/validators/storage.rb
mongoid-7.2.1 lib/mongoid/clients/validators/storage.rb
mongoid-7.1.7 lib/mongoid/clients/validators/storage.rb
mongoid-7.2.0 lib/mongoid/clients/validators/storage.rb
mongoid-7.1.6 lib/mongoid/clients/validators/storage.rb