Skip to content

Instantly share code, notes, and snippets.

@dbousamra
Created March 20, 2012 05:37
Show Gist options
  • Save dbousamra/2131706 to your computer and use it in GitHub Desktop.
Save dbousamra/2131706 to your computer and use it in GitHub Desktop.
import play.api._
import scala.io.Source
import models._
import com.codahale.jerkson.Json._
import com.mongodb.casbah.MongoConnection
import com.mongodb.casbah.commons.MongoDBObject
object Global extends GlobalSettings {
override def onStart(app: Application) = {
//removes existing data
//conn.drop()
parseMakes("mock_data/makes.json")
parseModels("mock_data/models.json")
}
def parseModels(filename: String) = {
val conn = MongoConnection()("cars")("models")
val json = scala.io.Source.fromFile(filename)
val models = parse[List[Model]](json)
models.foreach { model =>
val builder = MongoDBObject.newBuilder
builder += "id" -> model.id
builder += "name" -> model.name
builder += "make_id" -> model.make_id
conn += builder.result
}
}
def parseMakes(filename: String) = {
val conn = MongoConnection()("cars")("makes")
val json = scala.io.Source.fromFile(filename)
val makes = parse[List[Make]](json)
makes.foreach { make =>
val builder = MongoDBObject.newBuilder
builder += "id" -> make.id
builder += "name" -> make.name
conn += builder.result
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment