Class: MongoModel::Index

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

Instance Method Summary collapse

Constructor Details

#initialize(*keys) ⇒ 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)
  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

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

#keysObject


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

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

#to_argsObject


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 = []
  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?Boolean


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

def unique?
  @unique
end