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
-
MAPPINGS =
The mappings of nice Ruby-style names to the corresponding driver option name.
{ expire_after_seconds: :expire_after }
Class Method Summary
-
.new(klass, key, opts = nil) ⇒ Specification
constructor
Instantiate a new index specification.
Instance Attribute Summary
- #fields readonly
- #key ⇒ Hash rw
- #klass ⇒ Class rw
- #options rw
Instance Method Summary
-
#==(other) ⇒ true | false
Is this index specification equal to another?
-
#name ⇒ String
Get the index name, generated using the index key.
-
#normalize_aliases!(spec) ⇒ Hash
private
Internal use only
Internal use only
Normalize the spec in-place, in case aliased fields are provided.
-
#normalize_options!(options) ⇒ Hash
private
Internal use only
Internal use only
Normalize the index options in-place.
-
#recursive_normalize_conditionals!(options) ⇒ Hash | Array | Object
private
Internal use only
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.).
Constructor Details
.new(klass, key, opts = nil) ⇒ Specification
Instantiate a new index specification.
# File 'lib/mongoid/indexable/specification.rb', line 44
def initialize(klass, key, opts = nil) = opts || {} Validators::Options.validate(klass, key, ) @klass = klass @key = normalize_aliases!(key.dup) @fields = @key.keys @options = ( .deep_dup) end
Instance Attribute Details
#fields (readonly)
[ GitHub ]#key ⇒ Hash (rw)
#klass ⇒ Class
(rw)
#options (rw)
[ GitHub ]Instance Method Details
#==(other) ⇒ true
| false
Is this index specification equal to another?
#name ⇒ String
Get the index name, generated using the index key.
# 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)
Normalize the spec in-place, in case aliased fields are provided.
#normalize_options!(options) ⇒ Hash (private)
Normalize the index options in-place. Performs deep normalization on options which have a fields hash value.
# File 'lib/mongoid/indexable/specification.rb', line 96
def ( ) .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!( [key]) end end
#recursive_normalize_conditionals!(options) ⇒ Hash | Array | Object
(private)
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.)
# File 'lib/mongoid/indexable/specification.rb', line 124
def recursive_normalize_conditionals!( ) case when Hash normalize_aliases!( ) .keys.select { |key| key.to_s.start_with?('$') }.each do |key| recursive_normalize_conditionals!( [key]) end when Array .each { |opt| recursive_normalize_conditionals!(opt) } end end