class Mongoid::Indexable::Specification

Encapsulates behavior around an index specification.

@since 4.0.0

Constants

MAPPINGS

The mappings of nice Ruby-style names to the corresponding driver option name.

@since 4.0.0

Attributes

fields[R]

@!attribute klass

@return [ Class ] The class the index is defined on.

@!attribute key

@return [ Hash ] The index key.

@!attribute options

@return [ Hash ] The index options.
key[R]

@!attribute klass

@return [ Class ] The class the index is defined on.

@!attribute key

@return [ Hash ] The index key.

@!attribute options

@return [ Hash ] The index options.
klass[R]

@!attribute klass

@return [ Class ] The class the index is defined on.

@!attribute key

@return [ Hash ] The index key.

@!attribute options

@return [ Hash ] The index options.
options[R]

@!attribute klass

@return [ Class ] The class the index is defined on.

@!attribute key

@return [ Hash ] The index key.

@!attribute options

@return [ Hash ] The index options.

Public Class Methods

new(klass, key, opts = nil) click to toggle source

Instantiate a new index specification.

@example Create the new specification.

Specification.new(Band, { name: 1 }, background: true)

@param [ Class ] klass The class the index is defined on. @param [ Hash ] key The hash of name/direction pairs. @param [ Hash ] opts the index options.

@since 4.0.0

# File lib/mongoid/indexable/specification.rb, line 52
def initialize(klass, key, opts = nil)
  options = opts || {}
  Validators::Options.validate(klass, key, options)
  @klass = klass
  @key = normalize_key(key)
  @fields = @key.keys
  @options = normalize_options(options.dup)
end

Public Instance Methods

==(other) click to toggle source

Is this index specification equal to another?

@example Check equality of the specifications.

specification == other

@param [ Specification ] other The spec to compare against.

@return [ true, false ] If the specs are equal.

@since 4.0.0

# File lib/mongoid/indexable/specification.rb, line 38
def ==(other)
  fields == other.fields && key == other.key
end
name() click to toggle source

Get the index name, generated using the index key.

@example Get the index name.

specification.name

@return [ String ] name The index name.

@since 5.0.2

# File lib/mongoid/indexable/specification.rb, line 69
def name
  @name ||= key.reduce([]) do |n, (k,v)|
    n << "#{k}_#{v}"
  end.join('_')
end

Private Instance Methods

normalize_key(key) click to toggle source

Normalize the spec, in case aliased fields are provided.

@api private

@example Normalize the spec.

specification.normalize_key(name: 1)

@param [ Hash ] key The index key(s).

@return [ Hash ] The normalized specification.

@since 4.0.0

# File lib/mongoid/indexable/specification.rb, line 89
def normalize_key(key)
  normalized = {}
  key.each_pair do |name, direction|
    normalized[klass.database_field_name(name).to_sym] = direction
  end
  normalized
end
normalize_options(opts) click to toggle source

Normalize the index options, if any are provided.

@api private

@example Normalize the index options.

specification.normalize_options(drop_dups: true)

@param [ Hash ] opts The index options.

@return [ Hash ] The normalized options.

@since 4.0.0

# File lib/mongoid/indexable/specification.rb, line 109
def normalize_options(opts)
  options = {}
  opts.each_pair do |option, value|
    options[MAPPINGS[option] || option] = value
  end
  options
end