Skip to content

Instantly share code, notes, and snippets.

package com.knollop.store
import com.typesafe.config.{ConfigFactory, Config}
import com.mongodb.casbah.Imports._
import annotation.tailrec
trait MongoConfig {
val host: String
val defaultDb: String
lazy val mongo = MongoConnection(host)
package com.knollop.store.sb
import scalaz._
import scalaz.Validation
import scalaz.syntax.validation._
trait Driver {
sealed trait KError
case object NotFound extends KError
package com.knollop.store.sb
import scalaz._
import scalaz.Validation
import scalaz.syntax.validation._
trait Driver {
sealed trait KError
case object NotFound extends KError
trait Validator {
val errorMsg: String
def error: Option[String] = if(isGood) None else errorMsg
def isGood: Boolean
def ~(next: Validator): ValidationSet = ValidationSet(this, next)
}
case class DefinedValidator(value: Option[_], errorMsg: String) extends Validator {
def isGood = value.isDefined
case class OptionValidator[T](maybe: Option[T], innerF: T => Validator) {
val inner = maybe.map(v => innerF(v))
def isGood = inner.map(v => v.isGood).getOrElse(true)
def error: Option[String] = inner.flatMap(v => v.error)
val errorMsg = ""
}
// EXAMPLE
trait Athlete { self =>
// COMPILES, generates invalid SQL
def fetchProfileInfo(userId: Int) =
(for {
(profile, hometown) <- MemberProfiles leftJoin Geographies on (_.hometown_id === _.id)
(_, highSchool) <- profiles leftJoin Schools on (_.highschool_id === _.id)
(_, college) <- profiles leftJoin Schools on (_.college_id === _.id)
} yield {
(profile, hometown.name.?, highSchool.name.?, college.name.?)
}).filter(_._1.user_id === userId)
package com.sport195.api.models
import play.api.libs.json._
import com.sport195.api.common._
trait Mapped[T,X] {
def render(target: T, renderMode: RenderMode): X
}
class JsonMapped {
class CanBuild21[A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21](m1: M[A1 ~ A2 ~ A3 ~ A4 ~ A5 ~ A6 ~ A7 ~ A8 ~ A9 ~ A10 ~ A11 ~ A12 ~ A13 ~ A14 ~ A15 ~ A16 ~ A17 ~ A18 ~ A19 ~ A20], m2: M[A21]) {
def ~[A22](m3: M[A22]) = new CanBuild22[A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, A22](canBuild(m1, m2), m3)
def and[A22](m3: M[A22]) = this.~(m3)
def apply[B](f: (A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21) => B)(implicit fu: Functor[M]): M[B] =
fu.fmap[A1 ~ A2 ~ A3 ~ A4 ~ A5 ~ A6 ~ A7 ~ A8 ~ A9 ~ A10 ~ A11 ~ A12 ~ A13 ~ A14 ~ A15 ~ A16 ~ A17 ~ A18 ~ A19 ~ A20 ~ A21, B](canBuild(m1, m2), { case a1 ~ a2 ~ a3 ~ a4 ~ a5 ~ a6 ~ a7 ~ a8 ~ a9 ~ a10 ~ a11 ~ a12 ~ a13 ~ a14 ~ a15 ~ a16 ~ a17 ~ a18 ~ a19 ~ a20 ~ a21 => f(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21) })
def apply[B](f: B => (A1, A2,
package com.knollop.wharton.store.mongo
import play.api.libs.json._
import com.mongodb._
import com.knollop.wharton.store._
import org.bson.types.ObjectId
trait MCollection extends DBObjectTransformer {
def mongoDb: DB
val collectionName: String
val filename = "conf/my_file.txt"
val lines = scala.io.Source.fromFile(filename).getLines.toList
var linePtr = 0
def sendLine {
if(linePtr == lines.length) {
clock.cancel()
}
else {
testStream ! Message(lines(linePtr))