Class: Mongoid::Indexable::Specification

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

Overview

Encapsulates behavior around an index specification.

Constant Summary collapse

MAPPINGS =

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

{
  expire_after_seconds: :expire_after
}

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.



59
60
61
62
63
64
65
66
# File 'lib/mongoid/indexable/specification.rb', line 59

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

#fieldsObject (readonly)

Returns the value of attribute fields.



19
20
21
# File 'lib/mongoid/indexable/specification.rb', line 19

def fields
  @fields
end

#keyHash

Returns The index key.

Returns:

  • (Hash)

    The index key.



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

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

#klassClass

Returns The class the index is defined on.

Returns:

  • (Class)

    The class the index is defined on.



19
20
21
# File 'lib/mongoid/indexable/specification.rb', line 19

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



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

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:

Returns:

  • (true | false)

    If the specs are equal.



29
30
31
# File 'lib/mongoid/indexable/specification.rb', line 29

def ==(other)
  superficial_match?(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.



74
75
76
77
78
# File 'lib/mongoid/indexable/specification.rb', line 74

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

#superficial_match?(key: {}, name: nil) ⇒ true | false

Performs a superficial comparison with the given criteria, checking only the key and (optionally) the name. Options are not compared.

Note that the ordering of the fields in the key is significant. Two keys with different orderings will not match, here.

Parameters:

  • key (Hash) (defaults to: {})

    the key that defines the index.

  • name (String | nil) (defaults to: nil)

    the name given to the index, or nil to ignore the name.

Returns:

  • (true | false)

    the result of the comparison, true if this specification matches the criteria, and false otherwise.



45
46
47
48
49
# File 'lib/mongoid/indexable/specification.rb', line 45

def superficial_match?(key: {}, name: nil)
  (name && name == self.name) ||
    (fields == key.keys &&
      self.key == key)
end