Sha256: 133928d8129b356004b83ca3d8f977c9fcae63fe329b4ea7b535c3e2bd4cbc36

Contents?: true

Size: 977 Bytes

Versions: 5

Compression:

Stored size: 977 Bytes

Contents

module MongoDoc
  module Index

    # Create an index on a collection. For a unique index, pass the unique
    # option +:unique => true+. For compound indexes, pass pairs of fields and
    # directions (+:asc+, +:desc+) as a hash.
    #
    # <tt>Person.index(:last_name)</tt>
    # <tt>Person.index(:ssn, :unique => true)</tt>
    # <tt>Person.index(:first_name => :asc, :last_name => :asc)</tt>
    # <tt>Person.index(:first_name => :asc, :last_name => :asc, :unique => true)</tt>
    def index(*args)
      options_and_fields = args.extract_options!
      unique = options_and_fields.delete(:unique) || false
      if args.any?
        collection.create_index(args.first, unique)
      else
        collection.create_index(to_mongo_direction(options_and_fields), unique)
      end
    end

    protected
    def to_mongo_direction(fields_hash)
      fields_hash.to_a.map {|field| [field.first, field.last == :desc ? Mongo::DESCENDING : Mongo::ASCENDING]}
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongo_doc-0.6.1 lib/mongo_doc/index.rb
mongo_doc-0.6.0 lib/mongo_doc/index.rb
mongo_doc-0.5.5 lib/mongo_doc/index.rb
mongo_doc-0.4.2 lib/mongo_doc/index.rb
mongo_doc-0.4.1 lib/mongo_doc/index.rb