Permalink
Browse files

[reactivemongo] Documentation for pattern matching on write operator

  • Loading branch information...
cchantep
cchantep committed Sep 15, 2014
1 parent 04355e9 commit 357c67663436dfa73f82e2b329c4fb7e559b447d
Showing with 25 additions and 8 deletions.
  1. +0 −1 .travis.yml
  2. +13 −1 reactive-mongo/readme.md
  3. +12 −6 reactive-mongo/src/test/scala/acolyte/reactivemongo/MongoDBSpec.scala
View
@@ -3,5 +3,4 @@ jdk:
- openjdk6
env: JAVA_OPTS="-Xms32m -Xmx128m"
scala:
- 2.10.4
- 2.11.2
View
@@ -32,7 +32,7 @@ import reactivemongo.bson.{ BSONInteger, BSONString }
import acolyte.reactivemongo.{ CollectionName, RequestBody, Property, & }
request match {
queryRequest match {
case RequestBody("a-mongo-db.a-col-name", _) =>
// Any request on collection "a-mongo-db.a-col-name"
resultA
@@ -108,6 +108,18 @@ request match {
}
```
In case of write operation, handler is given the write operator along with the request itself, so dispatch can be based on this information (and combine with pattern matching on request content).
```scala
import acolyte.reactivemongo.{ DeleteOp, InsertOp, UpdateOp }
(operator, writeRequest) match {
case (DeleteOp, RequestBody("a-mongo-db.a-col-name", _)) => resultDelete
case (InsertOp, _) => resultInsert
case (UpdateOp, _) => resultUpdate
}
```
### Result creation for queries
Mongo result to be returned by query handler, can be created as following:
@@ -56,18 +56,24 @@ object MongoDBSpec extends org.specs2.mutable.Specification with MongoFixtures {
"Write operator" should {
"be delete" in {
MongoDB.WriteOp(Delete("db.col", 0)) aka "parsed op" must beSome.which(
_ aka "write op" must_== DeleteOp)
MongoDB.WriteOp(Delete("db.col", 0)) aka "parsed op" must beSome.
which(op op aka "write op" must_== DeleteOp and (op must beLike {
case DeleteOp ok // check pattern matching
}))
}
"be insert" in {
MongoDB.WriteOp(Insert(1, "db.col")) aka "parsed op" must beSome.which(
_ aka "write op" must_== InsertOp)
MongoDB.WriteOp(Insert(1, "db.col")) aka "parsed op" must beSome.
which(op op aka "write op" must_== InsertOp and (op must beLike {
case InsertOp ok // check pattern matching
}))
}
"be update" in {
MongoDB.WriteOp(Update("db.col", 2)) aka "parsed op" must beSome.which(
_ aka "write op" must_== UpdateOp)
MongoDB.WriteOp(Update("db.col", 2)) aka "parsed op" must beSome.
which(op op aka "write op" must_== UpdateOp and (op must beLike {
case UpdateOp ok // check pattern matching
}))
}
}

0 comments on commit 357c676

Please sign in to comment.