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

https://github.com/d5nguyenvan/casbah · Scala · 110 lines · 55 code · 17 blank · 38 comment · 0 complexity · 82fc89317033080783a866adc8ef2319 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 = new BasicDBObject(map.asJava)
  47. implicit def wrapDBObj(in: DBObject): MongoDBObject =
  48. new MongoDBObject { val underlying = in }
  49. implicit def unwrapDBObj(in: MongoDBObject): DBObject =
  50. in.underlying
  51. implicit def wrapDBList(in: BasicDBList): MongoDBList =
  52. new MongoDBList { val underlying = in }
  53. implicit def unwrapDBList(in: MongoDBList): BasicDBList =
  54. in.underlying
  55. // Register the core Serialization helpers.
  56. com.mongodb.casbah.commons.conversions.scala.RegisterConversionHelpers()
  57. }
  58. object Implicits extends Implicits
  59. object Imports extends Imports
  60. object BaseImports extends BaseImports
  61. object TypeImports extends TypeImports
  62. trait Imports extends BaseImports with TypeImports with Implicits
  63. trait BaseImports {
  64. val MongoDBObject = com.mongodb.casbah.commons.MongoDBObject
  65. val DBObject = MongoDBObject
  66. val MongoDBList = com.mongodb.casbah.commons.MongoDBList
  67. val DBList = MongoDBList
  68. }
  69. trait TypeImports {
  70. type MongoDBObject = com.mongodb.casbah.commons.MongoDBObject
  71. type MongoDBList = com.mongodb.casbah.commons.MongoDBList
  72. type DBObject = com.mongodb.DBObject
  73. type BasicDBObject = com.mongodb.BasicDBObject
  74. type BasicDBList = com.mongodb.BasicDBList
  75. type ObjectId = org.bson.types.ObjectId
  76. type DBRef = com.mongodb.DBRef
  77. type MongoException = com.mongodb.MongoException
  78. }
  79. abstract class ValidBSONType[T]
  80. object ValidBSONType {
  81. implicit object BasicBSONList extends ValidBSONType[org.bson.types.BasicBSONList]
  82. implicit object BasicDBList extends ValidBSONType[com.mongodb.BasicDBList]
  83. implicit object Binary extends ValidBSONType[org.bson.types.Binary]
  84. implicit object BSONTimestamp extends ValidBSONType[org.bson.types.BSONTimestamp]
  85. implicit object Code extends ValidBSONType[org.bson.types.Code]
  86. implicit object CodeWScope extends ValidBSONType[org.bson.types.CodeWScope]
  87. implicit object ObjectId extends ValidBSONType[org.bson.types.ObjectId]
  88. implicit object Symbol extends ValidBSONType[org.bson.types.Symbol]
  89. implicit object BSONObject extends ValidBSONType[org.bson.BSONObject]
  90. implicit object BasicDBObject extends ValidBSONType[com.mongodb.BasicDBObject]
  91. implicit object DBObject extends ValidBSONType[com.mongodb.DBObject]
  92. }
  93. // vim: set ts=2 sw=2 sts=2 et: