Class: MongoModel::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomodel/document/indexes.rb

Instance Method Summary collapse

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)
  options = keys.extract_options!
  
  @unique = options.delete(:unique)
  @min    = options.delete(:min)
  @max    = options.delete(:max)
  
  keys.each do |key|
    self.keys[key.to_sym] = :ascending
  end
  
  options.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

Returns:



79
80
81
# File 'lib/mongomodel/document/indexes.rb', line 79

def geo2d?
  @geo2d ||= keys.size == 1 && keys.values.first == :geo2d
end

#keysObject



71
72
73
# File 'lib/mongomodel/document/indexes.rb', line 71

def keys
  @keys ||= ActiveSupport::OrderedHash.new
end

#to_argsObject



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

Returns:



75
76
77
# File 'lib/mongomodel/document/indexes.rb', line 75

def unique?
  @unique
end