/casbah-commons/src/main/scala/Implicits.scala

https://github.com/derzzle/casbah · Scala · 110 lines · 53 code · 18 blank · 39 comment · 0 complexity · 51b2b945ee5fef50ca445dd83ee00b5b MD5 · raw file

  1. /**
  2. * Copyright (c) 2010 10gen, Inc. <http://10gen.com>
  3. * Copyright (c) 2009, 2010 Novus Partners, Inc. <http://novus.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * For questions and comments about this product, please see the project page at:
  18. *
  19. * http://github.com/mongodb/casbah
  20. *
  21. */
  22. package com.mongodb.casbah
  23. package commons
  24. import scalaj.collection.Imports._
  25. trait Implicits {
  26. import com.mongodb.{ DBObject, BasicDBObject, BasicDBList }
  27. /*
  28. * Placeholder Type Alias
  29. *
  30. * TODO - Make me a Type Class to define boundaries
  31. */
  32. type JSFunction = String
  33. /**
  34. * Implicit extension methods for Scala <code>Map[String, Any]</code>
  35. * to convert to Mongo DBObject instances.
  36. * Does not currently convert nested values.
  37. * @param map A map of [String, Any]
  38. */
  39. implicit def mapAsDBObject(map: scala.collection.Map[String, Any]) = new {
  40. /**
  41. * Return a Mongo <code>DBObject</code> containing the Map values
  42. * @return DBObject
  43. */
  44. def asDBObject = map2MongoDBObject(map)
  45. }
  46. implicit def map2MongoDBObject(map: scala.collection.Map[String, Any]): DBObject = MongoDBObject(map.toList)
  47. // implicit def iterable2DBObject(iter: Iterable[(String, Any)]): DBObject = MongoDBObject(iter.toList)
  48. implicit def wrapDBObj(in: DBObject): MongoDBObject =
  49. new MongoDBObject(in)
  50. implicit def unwrapDBObj(in: MongoDBObject): DBObject =
  51. in.underlying
  52. implicit def wrapDBList(in: BasicDBList): MongoDBList = new MongoDBList(in)
  53. implicit def unwrapDBList(in: MongoDBList): BasicDBList = in.underlying
  54. // Register the core Serialization helpers.
  55. com.mongodb.casbah.commons.conversions.scala.RegisterConversionHelpers()
  56. }
  57. object Implicits extends Implicits
  58. object Imports extends Imports
  59. object BaseImports extends BaseImports
  60. object TypeImports extends TypeImports
  61. trait Imports extends BaseImports with TypeImports with Implicits
  62. trait BaseImports {
  63. val MongoDBObject = com.mongodb.casbah.commons.MongoDBObject
  64. val DBObject = MongoDBObject
  65. val MongoDBList = com.mongodb.casbah.commons.MongoDBList
  66. val DBList = MongoDBList
  67. }
  68. trait TypeImports {
  69. type MongoDBObject = com.mongodb.casbah.commons.MongoDBObject
  70. type MongoDBList = com.mongodb.casbah.commons.MongoDBList
  71. type DBObject = com.mongodb.DBObject
  72. type BasicDBObject = com.mongodb.BasicDBObject
  73. type BasicDBList = com.mongodb.BasicDBList
  74. type ObjectId = org.bson.types.ObjectId
  75. type DBRef = com.mongodb.DBRef
  76. type MongoException = com.mongodb.MongoException
  77. }
  78. abstract class ValidBSONType[T]
  79. object ValidBSONType {
  80. implicit object BasicBSONList extends ValidBSONType[org.bson.types.BasicBSONList]
  81. implicit object BasicDBList extends ValidBSONType[com.mongodb.BasicDBList]
  82. implicit object Binary extends ValidBSONType[org.bson.types.Binary]
  83. implicit object BSONTimestamp extends ValidBSONType[org.bson.types.BSONTimestamp]
  84. implicit object Code extends ValidBSONType[org.bson.types.Code]
  85. implicit object CodeWScope extends ValidBSONType[org.bson.types.CodeWScope]
  86. implicit object ObjectId extends ValidBSONType[org.bson.types.ObjectId]
  87. implicit object Symbol extends ValidBSONType[org.bson.types.Symbol]
  88. implicit object BSONObject extends ValidBSONType[org.bson.BSONObject]
  89. implicit object BasicDBObject extends ValidBSONType[com.mongodb.BasicDBObject]
  90. implicit object DBObject extends ValidBSONType[com.mongodb.DBObject]
  91. }
  92. // vim: set ts=2 sw=2 sts=2 et: