class MongoModel::Index

Public Class Methods

new(*keys) click to toggle source
# File lib/mongomodel/document/indexes.rb, line 55
def initialize(*keys)
  options = keys.extract_options!

  @name   = options.delete(:name)
  @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

Public Instance Methods

==(other) click to toggle source
# File lib/mongomodel/document/indexes.rb, line 108
def ==(other)
  other.is_a?(Index) && to_args == other.to_args
end
geo2d?() click to toggle source
# File lib/mongomodel/document/indexes.rb, line 80
def geo2d?
  @geo2d ||= keys.size == 1 && keys.values.first == :geo2d
end
keys() click to toggle source
# File lib/mongomodel/document/indexes.rb, line 72
def keys
  @keys ||= ActiveSupport::OrderedHash.new
end
to_args() click to toggle source
# File lib/mongomodel/document/indexes.rb, line 84
def to_args
  args = []
  options = {}

  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
    options[:min] = @min
    options[:max] = @max
  end

  options[:unique] = true if unique?
  options[:name] = @name if @name

  args << options if options.any?
  args
end
unique?() click to toggle source
# File lib/mongomodel/document/indexes.rb, line 76
def unique?
  @unique
end