Sha256: 20b92d5e3a25da2d03c4634c24037dceb112b10535ebfe15a7dfc9b056589e55

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Clients
    module Validators

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

        # The valid options for storage.
        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.
        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.
        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.
        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

9 entries across 9 versions & 1 rubygems

Version Path
mongoid-8.0.10 lib/mongoid/clients/validators/storage.rb
mongoid-8.0.9 lib/mongoid/clients/validators/storage.rb
mongoid-8.0.8 lib/mongoid/clients/validators/storage.rb
mongoid-8.0.7 lib/mongoid/clients/validators/storage.rb
mongoid-8.0.6 lib/mongoid/clients/validators/storage.rb
mongoid-8.0.5 lib/mongoid/clients/validators/storage.rb
mongoid-8.0.4 lib/mongoid/clients/validators/storage.rb
mongoid-8.0.3 lib/mongoid/clients/validators/storage.rb
mongoid-8.0.2 lib/mongoid/clients/validators/storage.rb