module Mongoid::Indexable::Validators::Options

Validates the options passed to the index macro.

Constants

VALID_OPTIONS
VALID_TYPES

Public Instance Methods

validate(klass, spec, options) click to toggle source

Validate the index specification.

@example Validate the index spec.

Options.validate(Band, name: 1)

@param [ Class ] klass The model class. @param [ Hash ] spec The index specification. @param [ Hash ] options The index options.

@raise [ Errors::InvalidIndex ] If validation failed.

@since 3.0.0

# File lib/mongoid/indexable/validators/options.rb, line 58
def validate(klass, spec, options)
  validate_spec(klass, spec, options)
  validate_options(klass, spec, options)
end

Private Instance Methods

validate_options(klass, spec, options) click to toggle source

Validates the options of the index spec.

@api private

@example Validate the options.

Options.validate_options(Band, name: 1)

@param [ Class ] klass The model class. @param [ Hash ] spec The index specification. @param [ Hash ] options The index options.

@raise [ Errors::InvalidIndex ] If validation failed.

@since 3.0.0

# File lib/mongoid/indexable/validators/options.rb, line 79
def validate_options(klass, spec, options)
  options.each_pair do |name, value|
    unless VALID_OPTIONS.include?(name)
      raise Errors::InvalidIndex.new(klass, spec, options)
    end
  end
end
validate_spec(klass, spec, options) click to toggle source

Validates the index spec.

@api private

@example Validate the spec.

Options.validate_spec(Band, name: 1)

@param [ Class ] klass The model class. @param [ Hash ] spec The index specification. @param [ Hash ] options The index options.

@raise [ Errors::InvalidIndex ] If validation failed.

@since 3.0.0

# File lib/mongoid/indexable/validators/options.rb, line 101
def validate_spec(klass, spec, options)
  raise Errors::InvalidIndex.new(klass, spec, options) if !spec.is_a?(::Hash)
  spec.each_pair do |name, value|
    next if name == :options
    unless VALID_TYPES.include?(value)
      raise Errors::InvalidIndex.new(klass, spec, options)
    end
  end
end