Advertisement
Guest User

Untitled

a guest
Apr 14th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.58 KB | None | 0 0
  1.  
  2. // DQCategory.scala
  3. case class DQCategory (
  4.   id            : Option[BSONObjectID],
  5.   title         : String,
  6.   sentences     : List[DailyQuotesSentence]
  7. )
  8.  
  9. object DQCategory {
  10.  
  11.   implicit object DQCategoryBSONReader extends BSONDocumentReader[DQCategory] {
  12.     def read (doc: BSONDocument): DQCategory = {
  13.       DQCategory (
  14.         doc.getAs[BSONObjectID]("id"),
  15.         doc.getAs[String]("title").get,
  16.         doc.getAs[List[DailyQuotesSentence]]("sentences").toList.flatten
  17.       )
  18.     }
  19.   }
  20.  
  21. }
  22.  
  23. // DQSentence.scala
  24. case class DQSentence(
  25.   id            : Option[BSONObjectID],
  26.   link          : String,
  27.   xpath         : String,
  28.   content       : String,
  29.   categories    : List[DQCategory]
  30. )
  31.  
  32. object DQSentence {
  33.  
  34.   implicit object DQSentenceBSONReader extends BSONDocumentReader[DQSentence] {
  35.     def read (doc: BSONDocument): DQSentence = {
  36.       DQSentence (
  37.         doc.getAs[BSONObjectID]("id"),
  38.         doc.getAs[String]("link").get,
  39.         doc.getAs[String]("xpath").get,
  40.         doc.getAs[String]("content").get,
  41.         doc.getAs[List[DQCategory]]("categories").toList.flatten
  42.       )
  43.     }
  44.   }  
  45.  
  46.   implicit object DQSentenceBSONWriter extends BSONDocumentWriter[DQSentence] {
  47.     def write(sentence: DQSentence): BSONDocument = {
  48.       BSONDocument (
  49.         "id"              -> sentence.id.getOrElse(BSONObjectID.generate),
  50.         "link"            -> sentence.link,
  51.         "xpath"           -> sentence.xpath,
  52.         "content"         -> sentence.content,
  53.         "categories"      -> sentence.categories
  54.       )
  55.     }
  56.   }
  57.  
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement