123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Indexable::Specification

Relationships & Source Files
Inherits: Object
Defined in: lib/mongoid/indexable/specification.rb

Overview

Encapsulates behavior around an index specification.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(klass, key, opts = nil) ⇒ Specification

Instantiate a new index specification.

Examples:

Create the new specification.

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

Parameters:

  • klass (Class)

    The class the index is defined on.

  • key (Hash)

    The hash of name/direction pairs.

  • opts (Hash) (defaults to: nil)

    the index options.

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 44

def initialize(klass, key, opts = nil)
  options = opts || {}
  Validators::Options.validate(klass, key, options)
  @klass = klass
  @key = normalize_aliases!(key.dup)
  @fields = @key.keys
  @options = normalize_options!(options.deep_dup)
end

Instance Attribute Details

#fields (readonly)

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 22

attr_reader :klass, :key, :fields, :options

#keyHash (rw)

Returns:

  • (Hash)

    The index key.

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 22

attr_reader :klass, :key, :fields, :options

#klassClass (rw)

Returns:

  • (Class)

    The class the index is defined on.

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 22

attr_reader :klass, :key, :fields, :options

#options (rw)

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 22

attr_reader :klass, :key, :fields, :options

Instance Method Details

#==(other) ⇒ true | false

Is this index specification equal to another?

Examples:

Check equality of the specifications.

specification == other

Parameters:

  • other (Specification)

    The spec to compare against.

Returns:

  • (true | false)

    If the specs are equal.

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 32

def ==(other)
  fields == other.fields && key == other.key
end

#nameString

Get the index name, generated using the index key.

Examples:

Get the index name.

specification.name

Returns:

  • (String)

    name The index name.

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 59

def name
  @name ||= key.reduce([]) do |n, (k,v)|
    n << "#{k}_#{v}"
  end.join('_')
end

#normalize_aliases!(spec) ⇒ Hash (private)

This method is for internal use only.

Normalize the spec in-place, in case aliased fields are provided.

Examples:

Normalize the spec in-place.

specification.normalize_aliases!(name: 1)

Parameters:

  • spec (Hash)

    The index specification.

Returns:

  • (Hash)

    The normalized specification.

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 77

def normalize_aliases!(spec)
  return unless spec.is_a?(Hash)

  spec.transform_keys! do |name|
    klass.database_field_name(name).to_sym
  end
end

#normalize_options!(options) ⇒ Hash (private)

This method is for internal use only.

Normalize the index options in-place. Performs deep normalization on options which have a fields hash value.

Examples:

Normalize the index options in-place.

specification.normalize_options!(unique: true)

Parameters:

  • options (Hash)

    The index options.

Returns:

  • (Hash)

    The normalized options.

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 96

def normalize_options!(options)

  options.transform_keys! do |option|
    option = option.to_sym
    MAPPINGS[option] || option
  end

  %i[partial_filter_expression weights wildcard_projection].each do |key|
    recursive_normalize_conditionals!(options[key])
  end

  options
end

#recursive_normalize_conditionals!(options) ⇒ Hash | Array | Object (private)

This method is for internal use only.

Recursively normalizes the nested elements of an options hash in-place, to account for $and operator (and other potential $-prefixed operators which may be supported by MongoDB in the future.)

Examples:

Recursively normalize the index options in-place.

opts = { '$and' => [{ name: { '$eq' => 'Bob' } },
                    { age: { '$gte' => 20 } }] }
specification.recursive_normalize_conditionals!(opts)

Parameters:

  • options (Hash | Array | Object)

    The index options.

Returns:

  • (Hash | Array | Object)

    The normalized options.

[ GitHub ]

  
# File 'lib/mongoid/indexable/specification.rb', line 124

def recursive_normalize_conditionals!(options)
  case options
  when Hash
    normalize_aliases!(options)
    options.keys.select { |key| key.to_s.start_with?('$') }.each do |key|
      recursive_normalize_conditionals!(options[key])
    end
  when Array
    options.each { |opt| recursive_normalize_conditionals!(opt) }
  end

  options
end