Skip to content

Instantly share code, notes, and snippets.

@freewind
Created March 20, 2011 12:53
Show Gist options
  • Save freewind/878314 to your computer and use it in GitHub Desktop.
Save freewind/878314 to your computer and use it in GitHub Desktop.
This code can't be compiled, and have some errors
package models
import com.mongodb._
import com.osinka.mongodb._, shape._
class Question(val user: User) extends MongoObject with Oid {
var title: String = _
var comments: List[QComment] = Nil
}
object Question extends MongoObjectShape[Question] { shape =>
lazy val collection = db.getCollection("questions") of Question
lazy val user = Field.ref("user", User.collection, _.user)
lazy val title = Field.scalar("title", _.title, (q: Question, v: String) => q.title = v)
object comments extends ArrayEmbeddedField[QComment]("comments", _.comments, Some((q: Question, v: Seq[QComment]) => { q.comments = v.toList })) with QCommentIn[Question]
lazy override val * = user :: title :: comments :: Nil
override def factory(dbo: DBObject) = for (user(u) <- Some(dbo)) yield new Question(u)
}
class QComment(val question: Question) extends MongoObject with Oid {
var content: String = _
}
trait QCommentIn[T] extends ObjectIn[QComment, T] {
lazy val question = Field.ref("question", Question.collection, _.question)
lazy val content = Field.scalar("content", _.content, (c: QComment, v: String) => c.content = v)
lazy override val * = question :: content :: Nil
override def factory(dbo: DBObject) = for (question(q) <- Some(dbo)) yield new QComment(q)
}
object QComment extends ObjectShape[QComment] with QCommentIn[QComment] {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment