Skip to content

Instantly share code, notes, and snippets.

@saml
Created March 18, 2013 16:58
Show Gist options
  • Save saml/5188782 to your computer and use it in GitHub Desktop.
Save saml/5188782 to your computer and use it in GitHub Desktop.
case class MenuItemPortion(title: String, price: String)
object MenuItemPortion extends Convertable[MenuItemPortion] {
override def readFrom(dbo: MongoDBObject): Option[MenuItemPortion] = {
val title: Option[String] = dbo.getAs[String]("title")
val price: Option[String]= dbo.getAs[String]("price")
if (title.isEmpty && price.isEmpty) None
else Some(MenuItemPortion(title.getOrElse(""), price.getOrElse("")))
}
}
@jsuereth
Copy link

val values = 
   for {
      name <- Seq("title", "price") 
      value <- dbo.getAs[String](name)
  } yield name -> value

def value(name: String) = values.getOrElse(name, "")

if(values.isEmpty) None
else Some(MenuItemPortion(value("title"), value("price")))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment