Class: MongoModel::Index
- Inherits:
-
Object
- Object
- MongoModel::Index
- Defined in:
- lib/mongomodel/document/indexes.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #geo2d? ⇒ Boolean
-
#initialize(*keys) ⇒ Index
constructor
A new instance of Index.
- #keys ⇒ Object
- #to_args ⇒ Object
- #unique? ⇒ Boolean
Constructor Details
#initialize(*keys) ⇒ Index
Returns a new instance of Index.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mongomodel/document/indexes.rb', line 55 def initialize(*keys) = keys. @unique = .delete(:unique) @min = .delete(:min) @max = .delete(:max) keys.each do |key| self.keys[key.to_sym] = :ascending end .each do |key, order| self.keys[key.to_sym] = order end end |
Instance Method Details
#==(other) ⇒ Object
103 104 105 |
# File 'lib/mongomodel/document/indexes.rb', line 103 def ==(other) other.is_a?(Index) && to_args == other.to_args end |
#geo2d? ⇒ Boolean
79 80 81 |
# File 'lib/mongomodel/document/indexes.rb', line 79 def geo2d? @geo2d ||= keys.size == 1 && keys.values.first == :geo2d end |
#keys ⇒ Object
71 72 73 |
# File 'lib/mongomodel/document/indexes.rb', line 71 def keys @keys ||= ActiveSupport::OrderedHash.new end |
#to_args ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mongomodel/document/indexes.rb', line 83 def to_args args = [] if geo2d? args << [[keys.keys.first, Mongo::GEO2D]] elsif keys.size == 1 && keys.values.first == :ascending args << keys.keys.first else args << keys.map { |k, o| [k, o == :ascending ? Mongo::ASCENDING : Mongo::DESCENDING] }.sort_by { |k| k.first.to_s } end if geo2d? && @min && @max args << { :min => @min, :max => @max } elsif unique? args << { :unique => true } end args end |
#unique? ⇒ Boolean
75 76 77 |
# File 'lib/mongomodel/document/indexes.rb', line 75 def unique? @unique end |