Skip to content

Commit

Permalink
deprecated get APIs on Database and provided alternative named APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
debasishg committed Sep 10, 2009
1 parent eef2731 commit d6363bd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 35 deletions.
43 changes: 38 additions & 5 deletions src/main/scala/scouch/db/Database.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,24 @@ case class Db(couch: Couch, name: String) extends Request(couch / name) with Js
}
}

/** fetch by id, returns a tuple (id, rev)
@deprecated use <tt>getRef</tt> instead */
@deprecated def ref_by_id(id: String) =
this / encode(id) ># %(Symbol("_id") ? str, Symbol("_rev") ? str)

/** fetch by id, returns a tuple (id, rev) */
def ref_by_id(id: String) =
def getRef(id: String) =
this / encode(id) ># %(Symbol("_id") ? str, Symbol("_rev") ? str)

/** fetch by id as an instance of the class <tt>clazz</tt>.
Returns a Tuple3 of (id, rev, T) */
import JsBean._
import scala.reflect.Manifest

def by_id[T](id: String)(implicit m: Manifest[T]) =
/** get an entity of type <tt>T</tt> based on its id. Returns a
<tt>Tuple3</tt> of (id, ref, T)
@deprecated use <tt>get(id)</tt> instead */
@deprecated def by_id[T](id: String)(implicit m: Manifest[T]) =
this / encode(id) ># {
case s@_ =>
val (id, ref) = getIdAndRef(s)
Expand All @@ -110,9 +118,34 @@ case class Db(couch: Couch, name: String) extends Request(couch / name) with Js
}
}

/** fetch by id and rev as an instance of the class <tt>clazz</tt>.
Returns a Tuple3 of (id, rev, T) */
def by_id[T](id: String, rev: String)(implicit m: Manifest[T]) =
/** get an entity of type <tt>T</tt> based on its id. Returns a
<tt>Tuple3</tt> of (id, ref, T) */
def get[T](id: String)(implicit m: Manifest[T]) =
this / encode(id) ># {
case s@_ =>
val (id, ref) = getIdAndRef(s)
(id, ref, fromJSON(s, Some(m.erasure))) match {
case (Some(i), Some(r), x) => (i, r, x.asInstanceOf[T])
case (_, _, x) => (null, null, x.asInstanceOf[T])
}
}

/** get an entity of type <tt>T</tt> based on its id and rev. Returns a
<tt>Tuple3</tt> of (id, ref, T)
@deprecated use <tt>get(id, rev)</tt> instead */
@deprecated def by_id[T](id: String, rev: String)(implicit m: Manifest[T]) =
this / encode(id) <<? Map("rev" -> rev) ># {
case s@_ =>
val (id, ref) = getIdAndRef(s)
(id, ref, fromJSON(s, Some(m.erasure))) match {
case (Some(i), Some(r), x) => (i, r, x.asInstanceOf[T])
case (_, _, x) => (null, null, x.asInstanceOf[T])
}
}

/** get an entity of type <tt>T</tt> based on its id and rev. Returns a
<tt>Tuple3</tt> of (id, ref, T) */
def get[T](id: String, rev: String)(implicit m: Manifest[T]) =
this / encode(id) <<? Map("rev" -> rev) ># {
case s@_ =>
val (id, ref) = getIdAndRef(s)
Expand Down
48 changes: 24 additions & 24 deletions src/test/scala/scouch/db/SCouchDbSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
.size should equal (1)
}
it("query by id should fetch the document") {
val ir = http(test ref_by_id d._id)
val ir = http(test getRef d._id)
ir._1 should equal(d._id)
val sh = http(test.by_id[DesignDocument](d._id))
val sh = http(test.get[DesignDocument](d._id))
sh._1 should equal(d._id)
sh._2 should equal(ir._2)
sh._3._id should equal(sh._1)
Expand All @@ -84,9 +84,9 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
.size should equal (1)
}
it("re-query by id should fetch the updated document") {
val ir = http(test ref_by_id d._id)
val ir = http(test getRef d._id)
ir._1 should equal(d._id)
val sh = http(test.by_id[DesignDocument](d._id))
val sh = http(test.get[DesignDocument](d._id))
sh._1 should equal(d._id)
sh._2 should equal(nir._2)
sh._3._id should equal(sh._1)
Expand Down Expand Up @@ -133,17 +133,17 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
ir._1 should equal("sstop")
}
it("query by id should fetch a row") {
ii = http(test ref_by_id ir._1)
ii = http(test getRef ir._1)
ii._1 should equal("sstop")
}
it("query by id and class should construct an object of that class") {
val sh = http(test.by_id[Shop](ir._1))
val sh = http(test.get[Shop](ir._1))
sh._1 should equal(ii._1)
sh._2 should equal(ii._2)
sh._3.toString should equal(s.toString)
}
it("query by id, rev and class should construct an object of that class") {
val st = http(test.by_id[Shop](ir._1, ir._2))
val st = http(test.get[Shop](ir._1, ir._2))
st._1 should equal(ir._1)
st._2 should equal(ir._2)
st._3.toString should equal(s.toString)
Expand All @@ -156,7 +156,7 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
nir._2 should not equal(ir._2)
}
it("subsequent query by id and class should give changed value for the object's fields") {
val sh = http(test.by_id[Shop](ir._1))
val sh = http(test.get[Shop](ir._1))
sh._1 should equal(ii._1)
sh._2 should equal(nir._2)
sh._3.item should equal("television")
Expand All @@ -181,11 +181,11 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
http(test all_docs).size should equal(sz + 1)
}
it("query by id should fetch a row") {
ii = http(test ref_by_id ir._1)
ii = http(test getRef ir._1)
ii._1 should equal(ir._1)
}
it("query by id and class should construct an object of that class") {
val sh = http(test.by_id[Shop](ir._1))
val sh = http(test.get[Shop](ir._1))
sh._1 should equal(ii._1)
sh._2 should equal(ii._2)
sh._3.toString should equal(s.toString)
Expand All @@ -194,7 +194,7 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
sh._3.price should equal(32500)
}
it("query by id, rev and class should construct an object of that class") {
val st = http(test.by_id[Shop](ir._1, ir._2))
val st = http(test.get[Shop](ir._1, ir._2))
st._1 should equal(ir._1)
st._2 should equal(ir._2)
st._3.toString should equal(s.toString)
Expand All @@ -210,7 +210,7 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
nir._2 should not equal(ir._2)
}
it("subsequent query by id and class should give changed value for the object's fields") {
val sh = http(test.by_id[Shop](ir._1))
val sh = http(test.get[Shop](ir._1))
sh._1 should equal(ii._1)
sh._2 should equal(nir._2)
sh._3.item should equal("air-conditioner")
Expand All @@ -234,11 +234,11 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
ir._1 should equal("contact_1")
}
it("query by id should fetch a row") {
ii = http(test ref_by_id ir._1)
ii = http(test getRef ir._1)
ii._1 should equal("contact_1")
}
it("query by id and class should construct an object of that class") {
val sh = http(test.by_id[Contact](ir._1))
val sh = http(test.get[Contact](ir._1))
sh._1 should equal(ii._1)
sh._2 should equal(ii._2)
sh._3.toString should equal(c1.toString)
Expand All @@ -259,11 +259,11 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
ir._1 should equal("prog_scala")
}
it("query by id should fetch a row") {
ii = http(test ref_by_id ir._1)
ii = http(test getRef ir._1)
ii._1 should equal("prog_scala")
}
it("query by id and class should construct an object of that class") {
val sh = http(test.by_id[Book](ir._1))
val sh = http(test.get[Book](ir._1))
sh._1 should equal(ii._1)
sh._2 should equal(ii._2)
sh._3.toString should equal(book.toString)
Expand Down Expand Up @@ -303,9 +303,9 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
.size should equal (2)
}
it("query by id should fetch the document") {
val ir = http(test ref_by_id d._id)
val ir = http(test getRef d._id)
ir._1 should equal(d._id)
val sh = http(test.by_id[DesignDocument](d._id))
val sh = http(test.get[DesignDocument](d._id))
sh._1 should equal(d._id)
sh._2 should equal(ir._2)
sh._3._id should equal(sh._1)
Expand All @@ -325,9 +325,9 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
.size should equal (2)
}
it("re-query by id should fetch the updated document") {
val ir = http(test ref_by_id d._id)
val ir = http(test getRef d._id)
ir._1 should equal(d._id)
val sh = http(test.by_id[DesignDocument](d._id))
val sh = http(test.get[DesignDocument](d._id))
sh._1 should equal(d._id)
sh._2 should equal(nir._2)
sh._3._id should equal(sh._1)
Expand Down Expand Up @@ -362,7 +362,7 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
val vi = new View(mf, rf)

it("should get design document with id = _design/lunch") {
val ir = http(test.by_id[DesignDocument]("_design/lunch"))
val ir = http(test.get[DesignDocument]("_design/lunch"))
ir._1 should equal("_design/lunch")
val d = ir._3
val de = Doc(test, ir._1)
Expand All @@ -374,7 +374,7 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
nir._1 should equal(ir._1)
nir._2 should not equal(ir._2)

val new_ir = http(test.by_id[DesignDocument]("_design/lunch"))
val new_ir = http(test.get[DesignDocument]("_design/lunch"))
new_ir._3.views.keySet.size should equal(2)
}
}
Expand Down Expand Up @@ -491,7 +491,7 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
ir._1 should equal("sears")
}
it("query by id should fetch a row") {
ii = http(test ref_by_id ir._1)
ii = http(test getRef ir._1)
ii._1 should equal("sears")
}
it("sticking an attachment should be successful") {
Expand Down Expand Up @@ -522,7 +522,7 @@ class SCouchDbSpec extends Spec with ShouldMatchers with BeforeAndAfter {
http(d.getAttachment("foo") as_str) should equal(att)
}
it("query by id should fetch a row") {
ii = http(test ref_by_id "cc")
ii = http(test getRef "cc")
ii._1 should equal("cc")
}
it("change an attachment") {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/scouch/db/ScalaValidationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ScalaValidationSpec extends Spec with ShouldMatchers with BeforeAndAfter {

private def deleteAllDocs {
http(test all_docs)
.map(id => http(test ref_by_id id))
.map(id => http(test getRef id))
.foreach{ir => http(Doc(test, ir._1).delete(ir._2))}
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/scala/scouch/db/ScalaViewServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class ScalaViewServerSpec extends Spec with ShouldMatchers with BeforeAndAfter
ir._1 should equal(d._id)
}
it("query by id should fetch the document") {
val ir = http(test ref_by_id d._id)
val ir = http(test getRef d._id)
ir._1 should equal(d._id)
val sh = http(test.by_id[DesignDocument](d._id))
val sh = http(test.get[DesignDocument](d._id))
sh._1 should equal(d._id)
sh._2 should equal(ir._2)
sh._3._id should equal(sh._1)
Expand Down Expand Up @@ -125,9 +125,9 @@ class ScalaViewServerSpec extends Spec with ShouldMatchers with BeforeAndAfter
ir._1 should equal(d._id)
}
it("query by id should fetch the document") {
val ir = http(test ref_by_id d._id)
val ir = http(test getRef d._id)
ir._1 should equal(d._id)
val sh = http(test.by_id[DesignDocument](d._id))
val sh = http(test.get[DesignDocument](d._id))
sh._1 should equal(d._id)
sh._2 should equal(ir._2)
sh._3._id should equal(sh._1)
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/scouch/db/ViewServerWithObjectsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ViewServerWithObjectsSpec extends Spec with ShouldMatchers with BeforeAndA

// query by id to get back the object
// returns a tuple3 of (id, rev, object)
val (id, rev, item) = http(carDb.by_id[CarSaleItem]("bmw_mini_used_redwhiteblue"))
val (id, rev, item) = http(carDb.get[CarSaleItem]("bmw_mini_used_redwhiteblue"))

id should equal("bmw_mini_used_redwhiteblue")
item.make should equal("BMW")
Expand Down

0 comments on commit d6363bd

Please sign in to comment.