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 70 |
# File 'lib/mongomodel/document/indexes.rb', line 55 def initialize(*keys) = keys. @name = .delete(:name) @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
108 109 110 |
# File 'lib/mongomodel/document/indexes.rb', line 108 def ==(other) other.is_a?(Index) && to_args == other.to_args end |
#geo2d? ⇒ Boolean
80 81 82 |
# File 'lib/mongomodel/document/indexes.rb', line 80 def geo2d? @geo2d ||= keys.size == 1 && keys.values.first == :geo2d end |
#keys ⇒ Object
72 73 74 |
# File 'lib/mongomodel/document/indexes.rb', line 72 def keys @keys ||= ActiveSupport::OrderedHash.new end |
#to_args ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/mongomodel/document/indexes.rb', line 84 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 [:min] = @min [:max] = @max end [:unique] = true if unique? [:name] = @name if @name args << if .any? args end |
#unique? ⇒ Boolean
76 77 78 |
# File 'lib/mongomodel/document/indexes.rb', line 76 def unique? @unique end |