Class: Mongoid::Indexable::Specification

Inherits:
Object
  • Object
show all
Defined in:
build/mongoid-master/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.



43
44
45
46
47
48
49
50
# File 'build/mongoid-master/lib/mongoid/indexable/specification.rb', line 43

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

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



21
22
23
# File 'build/mongoid-master/lib/mongoid/indexable/specification.rb', line 21

def fields
  @fields
end

#keyHash

Returns The index key.

Returns:

  • (Hash)

    The index key.



21
# File 'build/mongoid-master/lib/mongoid/indexable/specification.rb', line 21

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

#klassClass

Returns The class the index is defined on.

Returns:

  • (Class)

    The class the index is defined on.



21
22
23
# File 'build/mongoid-master/lib/mongoid/indexable/specification.rb', line 21

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



21
# File 'build/mongoid-master/lib/mongoid/indexable/specification.rb', line 21

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.



31
32
33
# File 'build/mongoid-master/lib/mongoid/indexable/specification.rb', line 31

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.



58
59
60
61
62
# File 'build/mongoid-master/lib/mongoid/indexable/specification.rb', line 58

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