Class: Mongoid::Indexable::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/indexable/specification.rb

Overview

Encapsulates behaviour around an index specification.

Since:

  • 4.0.0

Constant Summary collapse

MAPPINGS =

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

Since:

  • 4.0.0

{
  bucket_size: :bucketSize,
  drop_dups: :dropDups,
  expire_after_seconds: :expireAfterSeconds
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(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.

Since:

  • 4.0.0



64
65
66
67
68
69
70
# File 'lib/mongoid/indexable/specification.rb', line 64

def initialize(klass, key, opts = nil)
  options = opts || {}
  Validators::Options.validate(klass, key, options)
  @klass = klass
  @key = normalize_key(key)
  @options = normalize_options(options.dup)
end

Instance Attribute Details

#keyHash

Returns The index key.

Returns:

  • (Hash)

    The index key.



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

attr_reader :klass, :key, :options

#klassClass

Returns The class the index is defined on.

Returns:

  • (Class)

    The class the index is defined on.



26
27
28
# File 'lib/mongoid/indexable/specification.rb', line 26

def klass
  @klass
end

#optionsObject

Since:

  • 4.0.0



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

attr_reader :klass, :key, :options

Instance Method Details

#==(other) ⇒ true, false

Is this index specification equal to another?

Examples:

Check equality of the specifications.

specification == other

Parameters:

Returns:

  • (true, false)

    If the specs are equal.

Since:

  • 4.0.0



38
39
40
# File 'lib/mongoid/indexable/specification.rb', line 38

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

#fieldsArray<Symbol>

Get an array of the fields, in order, that are part of the index.

Examples:

Get the index fields.

specification.fields

Returns:

  • (Array<Symbol>)

    The names of the fields.

Since:

  • 4.0.0



50
51
52
# File 'lib/mongoid/indexable/specification.rb', line 50

def fields
  key.keys
end