Sha256: 590e0750f48f9857d6f9b897319a8afbd75407c982cdd1e7472528e2b6f35933

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Indexes
      extend ActiveSupport::Concern

      module ClassMethods
        def ensure_index(spec, options = {})
          #TODO: should we emulate the mongo 1.x behaviour of caching attempts to create indexes?
          collection.indexes.create_one dealias_options(spec), options
        end

        def create_index(spec, options = {})
          collection.indexes.create_one dealias_options(spec), options
        end

        def drop_index(name)
          collection.indexes.drop_one name
        end

        def drop_indexes
          collection.indexes.drop_all
        end

      private

        def dealias_options(options)
          case options
          when Symbol, String
            {abbr(options) => 1}
          when Hash
            dealias_keys(options)
          when Array
            if options.first.is_a?(Hash)
              options.map {|o| dealias_options(o) }
            elsif options.first.is_a?(Array) # [[:foo, 1], [:bar, 1]]
              options.inject({}) {|acc, tuple| acc.merge(dealias_options(tuple))}
            else
              dealias_keys(Hash[*options])
            end
          else
            options
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mongo_mapper-0.17.0 lib/mongo_mapper/plugins/indexes.rb
mongo_mapper-0.16.0 lib/mongo_mapper/plugins/indexes.rb
mongo_mapper-0.15.6 lib/mongo_mapper/plugins/indexes.rb
mongo_mapper-0.15.5 lib/mongo_mapper/plugins/indexes.rb
mongo_mapper-0.15.4 lib/mongo_mapper/plugins/indexes.rb
mongo_mapper-0.15.3 lib/mongo_mapper/plugins/indexes.rb
mongo_mapper-0.15.2 lib/mongo_mapper/plugins/indexes.rb
mongo_mapper-0.15.1 lib/mongo_mapper/plugins/indexes.rb
mongo_mapper-0.15.0 lib/mongo_mapper/plugins/indexes.rb