lib/candy/crunch.rb in candy-0.0.2 vs lib/candy/crunch.rb in candy-0.1.0

- old
+ new

@@ -1,7 +1,7 @@ require 'mongo' -require 'etc' +require 'etc' # To get the current username for database default module Candy # All of the hard crunchy bits that connect us to a collection within a Mongo database. module Crunch @@ -93,9 +93,21 @@ # Returns the collection you gave, or creates a default collection named for the current class. def collection @collection ||= db.create_collection(name) end + # Creates an index on the specified property, with an optional direction specified as either :asc or :desc. + # (Note that this is deliberately a very simple method. If you want multi-key or unique indexes, just call + # #create_index directly on the collection.) + def index(property, direction=:asc) + case direction + when :asc then mongo_direction = Mongo::ASCENDING + when :desc then mongo_direction = Mongo::DESCENDING + else + raise TypeError, "Index direction should be :asc or :desc" + end + collection.create_index(property => mongo_direction) + end end module InstanceMethods end \ No newline at end of file