Permalink
Browse files

Merge branch 'master' of https://github.com/mongodb/casbah

  • Loading branch information...
teigen committed Jan 20, 2012
2 parents 1fdc0d9 + e5b44eb commit 5d0d43095a989e90a43c607df4ebf5aee89e662f
@@ -91,6 +91,26 @@ class MongoDBObject(val underlying: DBObject = new BasicDBObject) extends scala.
super.++(other: DBObject)
}
+ /**
+ * Returns a new list with this MongoDBObject at the *end*
+ * Currently only supports composing with other DBObjects, primarily for
+ * the use of the Query DSL; if you want heterogenous lists, use
+ * MongoDBList directly.
+ * @see MongoDBList
+ *
+ */
+ def ::[A <% DBObject](elem: A): Seq[DBObject] = Seq(elem: DBObject, this)
+
+ /**
+ * Returns a new list with this MongoDBObject at the *end*
+ * Currently only supports composing with other DBObjects, primarily for
+ * the use of the Query DSL; if you want heterogenous lists, use
+ * MongoDBList directly.
+ * @see MongoDBList
+ *
+ */
+ def ::(elem: (String, Any)): Seq[DBObject] = Seq(MongoDBObject(elem), this)
+
/** Lazy utility method to allow typing without conflicting with Map's required get() method and causing ambiguity */
def getAs[A <: Any: Manifest](key: String): Option[A] = {
require(manifest[A] != manifest[scala.Nothing],
@@ -18,19 +18,22 @@
package com.mongodb.casbah.commons
package test
-import org.specs2.mutable._
+import org.specs2._
import org.specs2.data.Sized
import org.specs2.matcher.{ Expectable, Matcher, MapMatchers }
import org.specs2.matcher.Matchers._
import com.mongodb.casbah.util.Logging
import com.mongodb.casbah.commons.MongoDBObject
-import javax.management.remote.rmi._RMIConnection_Stub
object `package` {
}
-trait CasbahSpecification extends Specification with DBObjectMatchers with Logging {
+trait CasbahMutableSpecification extends mutable.Specification with CasbahSpecificationBase
+
+trait CasbahSpecification extends Specification with CasbahSpecificationBase
+
+trait CasbahSpecificationBase extends DBObjectMatchers with Logging {
implicit val sizedOptDBObj = new Sized[Option[DBObject]] {
def size(t: Option[DBObject]) = t.getOrElse(MongoDBObject.empty).size
}
@@ -23,9 +23,9 @@
package com.mongodb.casbah.test.commons
import com.mongodb.casbah.commons._
-import com.mongodb.casbah.commons.test.CasbahSpecification
+import com.mongodb.casbah.commons.test.CasbahMutableSpecification
-class MongoDBListSpec extends CasbahSpecification {
+class MongoDBListSpec extends CasbahMutableSpecification {
val x = Seq(5, 9, 212, "x", "y", 22.98)
val y = Seq("spam", "eggs", "foo", "bar")
@@ -23,9 +23,9 @@
package com.mongodb.casbah.test.commons
import com.mongodb.casbah.commons._
-import com.mongodb.casbah.commons.test.CasbahSpecification
+import com.mongodb.casbah.commons.test.CasbahMutableSpecification
-class MongoDBObjectSpec extends CasbahSpecification {
+class MongoDBObjectSpec extends CasbahMutableSpecification {
"MongoDBObject expand operations" should {
@@ -249,6 +249,27 @@ class MongoDBObjectSpec extends CasbahSpecification {
explicit.hashCode must beEqualTo(control.hashCode())
explicit.equals(explicit) must beEqualTo(control.equals(control))
}
+
+ "Support list creation operators" in {
+ "Prepend to end of a new list" in {
+ "With explicitly created Elements" in {
+ val list = MongoDBObject("x" -> "y") :: MongoDBObject("foo" -> "bar")
+ list must haveSize(2)
+ list(0) must beDBObject
+ (list(0): DBObject) must haveEntries("x" -> "y")
+ list(1) must beDBObject
+ (list(1): DBObject) must haveEntries("foo" -> "bar")
+ }
+ "With implicitly created Elements with an explicit" in {
+ val list = ("x" -> "y") :: MongoDBObject("foo" -> "bar")
+ list must haveSize(2)
+ list(0) must beDBObject
+ (list(0): DBObject) must haveEntries("x" -> "y")
+ list(1) must beDBObject
+ (list(1): DBObject) must haveEntries("foo" -> "bar")
+ }
+ }
+ }
}
}
@@ -117,6 +117,12 @@ object Imports extends Imports with commons.Imports with commons.Exports with qu
trait Imports extends BaseImports with TypeImports with Implicits
package core {
+ /**
+ * You can import core to get "just" Casbah core; no commons, query, etc.
+ * This is useful to pick which QueryDSL type you want, etc.
+ */
+ object `package` extends Imports
+
trait Exports {
type MongoCursor = com.mongodb.casbah.MongoCursor
type MongoCollection = com.mongodb.casbah.MongoCollection
@@ -44,7 +44,7 @@ object MongoURI {
*
* @param uri (String)
*/
- def apply(uri: String) = new MongoURI(new com.mongodb.MongoURI(uri))
+ def apply(uri: String): MongoURI = new MongoURI(new com.mongodb.MongoURI(uri))
}
/**
@@ -60,20 +60,38 @@ object MongoURI {
* @since 2.0
*/
class MongoURI(val underlying: com.mongodb.MongoURI) {
- def username = underlying.getUsername
- def password = underlying.getPassword
- def hosts = underlying.getHosts.asScala
- def database = underlying.getDatabase
- def collection = underlying.getCollection
- def options = underlying.getOptions
- def connect = underlying.connect.asScala
- def connectDB = underlying.connectDB.asScala
- def connectDB(m: MongoConnection) =
- underlying.connectDB(m.underlying).asScala
- def connectCollection(db: MongoDB) =
- underlying.connectCollection(db.underlying).asScala
- def connectCollection(m: MongoConnection) =
- underlying.connectCollection(m.underlying).asScala
+ def username: Option[String] = Option(underlying.getUsername)
+ def password: Option[Array[Char]] = Option(underlying.getPassword)
+ def hosts: Seq[String] = underlying.getHosts.asScala
+ def database: Option[String] = Option(underlying.getDatabase)
+ def collection: Option[String] = Option(underlying.getCollection)
+ def options: MongoOptions = underlying.getOptions
+ def connect: Either[Throwable, MongoConnection] = try {
+ Right(underlying.connect.asScala)
+ } catch {
+ case t => Left(t)
+ }
+ def connectDB: Either[Throwable, MongoDB] = {
+ try {
+ require(database.isDefined, "Cannot connect to Database as none is defined.")
+ Right(underlying.connectDB.asScala)
+ } catch {
+ case t => Left(t)
+ }
+ }
+ def connectCollection: Either[Throwable, MongoCollection] = {
+ try {
+ require(collection.isDefined, "Cannot connect to Collection as none is defined.")
+ connectDB match {
+ case Right(db) =>
+ Right(db(collection.get))
+ case Left(t) => Left(t)
+ }
+ } catch {
+ case t => Left(t)
+ }
+
+ }
override def toString = underlying.toString
}
@@ -27,11 +27,11 @@ import com.mongodb.casbah.commons.conversions.scala._
import com.mongodb.casbah._
import org.scala_tools.time.Imports._
-import com.mongodb.casbah.commons.test.CasbahSpecification
+import com.mongodb.casbah.commons.test.CasbahMutableSpecification
import org.specs2.specification.BeforeExample
import org.bson.BSON
-class ConversionsSpec extends CasbahSpecification with BeforeExample {
+class ConversionsSpec extends CasbahMutableSpecification with BeforeExample {
type JDKDate = java.util.Date
@@ -24,9 +24,9 @@ package com.mongodb.casbah
import com.mongodb.casbah._
-import com.mongodb.casbah.commons.test.CasbahSpecification
+import com.mongodb.casbah.commons.test.CasbahMutableSpecification
-class CoreWrappersSpec extends CasbahSpecification {
+class CoreWrappersSpec extends CasbahMutableSpecification {
"Casbah behavior between Scala and Java versions of Objects" should {
@@ -26,9 +26,9 @@ import com.mongodb.casbah.util.Logging
import com.mongodb.casbah.commons.conversions.scala._
import org.scala_tools.time.Imports._
-import com.mongodb.casbah.commons.test.CasbahSpecification
+import com.mongodb.casbah.commons.test.CasbahMutableSpecification
-class GroupSpec extends CasbahSpecification {
+class GroupSpec extends CasbahMutableSpecification {
"Casbah's Group Interfaces" should {
implicit val mongoDB = MongoConnection()("casbahIntegration")
@@ -21,14 +21,14 @@
*/
package com.mongodb.casbah.test
-import com.mongodb.casbah.commons.test.CasbahSpecification
+import com.mongodb.casbah.commons.test.CasbahMutableSpecification
import com.mongodb.casbah._
import com.mongodb.casbah.util.bson.decoding.OptimizedLazyDBObject
import java.util.{UUID , Date}
import org.bson.types.{MinKey, MaxKey, BSONTimestamp, Binary}
import java.util.regex.Pattern
-class LazyDecodingSpec extends CasbahSpecification {
+class LazyDecodingSpec extends CasbahMutableSpecification {
implicit val mongoInt = MongoConnection()("casbahIntegration")
implicit val mongoTest = MongoConnection()("casbahTest_Lazy")
@@ -163,7 +163,7 @@ class LazyDecodingSpec extends CasbahSpecification {
doc must haveEntry("double245_6289" -> 245.6289 )
doc must haveEntry("oid" -> testOid )
doc must haveEntry("str" -> "foobarbaz" )
- //doc.getAs[DBRef]("ref") must beSome( new DBRef(mongoInt.underlying, "books", testRefOid) )
+ doc must haveEntry("uuid" -> testUUID)
doc must haveEntry("object.abc" -> testDoc.get("abc") )
doc.getAs[Pattern]("regex").get.pattern() must_==( testRE.pattern.pattern() )
}
@@ -27,10 +27,10 @@ import com.mongodb.casbah.util.Logging
import com.mongodb.casbah.commons.conversions.scala._
import org.scala_tools.time.Imports._
-import com.mongodb.casbah.commons.test.CasbahSpecification
+import com.mongodb.casbah.commons.test.CasbahMutableSpecification
@SuppressWarnings(Array("deprecation"))
-class MapReduceSpec extends CasbahSpecification {
+class MapReduceSpec extends CasbahMutableSpecification {
"Casbah's Map/Reduce Engine" should {
@@ -27,10 +27,10 @@ import com.mongodb.casbah.util.Logging
import com.mongodb.casbah.commons.conversions.scala._
import org.scala_tools.time.Imports._
-import com.mongodb.casbah.commons.test.CasbahSpecification
+import com.mongodb.casbah.commons.test.CasbahMutableSpecification
@SuppressWarnings(Array("deprecation"))
-class RawMapReduceSpec extends CasbahSpecification {
+class RawMapReduceSpec extends CasbahMutableSpecification {
"MongoDB 1.7+ Map/Reduce functionality" should {
implicit val mongoDB = MongoConnection()("casbahIntegration")
@@ -29,10 +29,10 @@ import com.mongodb.casbah.gridfs._
import java.security.MessageDigest
import java.io._
-import com.mongodb.casbah.commons.test.CasbahSpecification
+import com.mongodb.casbah.commons.test.CasbahMutableSpecification
-class GridFSSpec extends CasbahSpecification {
+class GridFSSpec extends CasbahMutableSpecification {
/* override def is = args(sequential = true) ^ super.is*/
implicit val mongo = MongoConnection()("casbah_test")
Oops, something went wrong.

0 comments on commit 5d0d430

Please sign in to comment.