Class: Mongoid::Indexable::Specification
- Inherits:
-
Object
- Object
- Mongoid::Indexable::Specification
- 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
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#key ⇒ Hash
The index key.
-
#klass ⇒ Class
The class the index is defined on.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#==(other) ⇒ true | false
Is this index specification equal to another?.
-
#initialize(klass, key, opts = nil) ⇒ Specification
constructor
Instantiate a new index specification.
-
#name ⇒ String
Get the index name, generated using the index key.
-
#superficial_match?(key: {}, name: nil) ⇒ true | false
Performs a superficial comparison with the given criteria, checking only the key and (optionally) the name.
Constructor Details
#initialize(klass, key, opts = nil) ⇒ Specification
Instantiate a new index specification.
59 60 61 62 63 64 65 66 |
# File 'lib/mongoid/indexable/specification.rb', line 59 def initialize(klass, key, opts = nil) = opts || {} Validators::Options.validate(klass, key, ) @klass = klass @key = normalize_aliases!(key.dup) @fields = @key.keys = (.deep_dup) end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
19 20 21 |
# File 'lib/mongoid/indexable/specification.rb', line 19 def fields @fields end |
#key ⇒ Hash
Returns The index key.
19 |
# File 'lib/mongoid/indexable/specification.rb', line 19 attr_reader :klass, :key, :fields, :options |
#klass ⇒ Class
Returns The class the index is defined on.
19 20 21 |
# File 'lib/mongoid/indexable/specification.rb', line 19 def klass @klass end |
#options ⇒ Object
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?
29 30 31 |
# File 'lib/mongoid/indexable/specification.rb', line 29 def ==(other) superficial_match?(key: other.key) end |
#name ⇒ String
Get the index name, generated using the index key.
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.
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 |