Sha256: d8b4a1bf34e1de394970ed7ad3a766e3f3446f1b66f273801c58acba6181f31e

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

# encoding: utf-8
module Mongoid
  module Sessions
    module Validators

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

        VALID_OPTIONS = [ :collection, :database, :session ]

        # 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)
          if !valid_keys?(options) || !valid_parent?(klass)
            raise Errors::InvalidStorageOptions.new(klass, options)
          end
        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

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-4.0.0.beta1 lib/mongoid/sessions/validators/storage.rb
mongoid-4.0.0.alpha2 lib/mongoid/sessions/validators/storage.rb
mongoid-4.0.0.alpha1 lib/mongoid/sessions/validators/storage.rb