Skip to content

Instantly share code, notes, and snippets.

@eamelink
eamelink / Build.scala
Last active August 29, 2015 13:57
Compiling templates to an object that throws a runtime error. Useful for large refactorings, where you temporarily don't want to be bothered by compilation errors in templates.
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "fake-templates"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
trait ElasticSearchComponent {
val esClient: EsClient
}
trait ElasticSearchIndex { self: ElasticSearchComponent =>
def findAll() = esClient.doAQueryForAll()
}
@eamelink
eamelink / Application.scala
Created November 15, 2013 12:31
Proxying a web service with Play
package controllers
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.iteratee.{ Concurrent, Iteratee }
import play.api.libs.ws.{ ResponseHeaders, WS }
import play.api.mvc.{ Action, Controller, SimpleResult }
import scala.concurrent.Promise
object Application extends Controller {
@eamelink
eamelink / iteratees-by-example.scala
Created May 23, 2013 21:40
Scala-IDE worksheet with some examples of iteratees, enumerators and enumeratees and how to use and compose them. Originated from a presentation at Dutch Scala Enthusiasts.
import play.api.libs.iteratee._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._
import play.api.libs.concurrent.Promise
object iteratees {
// Implicit conversion to add 'await' to a Future
implicit class WFuture[A](val inner: Future[A]) extends AnyVal {
@eamelink
eamelink / fields.scala
Last active December 13, 2015 17:18
Checking how constructor parameters do or do not end up as fields
/*
* bar is not a real field: after the lazy val bork is initialized,
* 'bar' is set to null.
*
* It's a normal field when we make bork a def, add 'val' or 'var' to bar or make it a case class.
* It's not a field at all when we make bork a val.
*/
class MyClass(param: String) {
lazy val field = param
@eamelink
eamelink / ErrorReps.scala
Created January 29, 2013 10:28
Scala 'rep' parser combinators that cascade 'Error's. The regular ones swallow any error that are not in the first repetition.
trait ErrorReps {
// Copied and adapted from Scala 2.10.0
def repErr[T](p: => Parser[T]): Parser[List[T]] = rep1Err(p) | success(List())
def rep1Err[T](p: => Parser[T]): Parser[List[T]] = rep1Err(p, p)
def rep1Err[T](first: => Parser[T], p0: => Parser[T]): Parser[List[T]] = Parser { in =>
lazy val p = p0 // lazy argument
val elems = new ListBuffer[T]
@eamelink
eamelink / gist:3947127
Created October 24, 2012 16:26
Chef odd cookbook_file mode
This is in my cookbook:
cookbook_file "/tmp/cleanup.sh" do
mode "0744"
end
And this is what Chef logs:
[2012-10-24T17:58:28+02:00] INFO: Processing cookbook_file[/tmp/cleanup.sh] action create (basebox::basebox-cleanup line 1)
[2012-10-24T17:58:28+02:00] INFO: cookbook_file[/tmp/cleanup.sh] owner changed to 0
@eamelink
eamelink / gist:3742696
Created September 18, 2012 11:32
Play 2 Scala interceptors
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
super.onRouteRequest(request).map {
case action: Action[body] => Action[body](action.parser) { (request: Request[body]) =>
println("Before!")
val result = action.apply(request)
result match {
case AsyncResult(resultPromise) => resultPromise.onRedeem { _ =>
println("After!")
}
case otherResult => {
@eamelink
eamelink / jsonformtest.scala
Created March 9, 2012 20:35
Play2 Form handling of json with missing property vs null
import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
import play.api.data._
import play.api.data.Forms._
import play.api.libs.json.Json
class JsonBindingSpec extends Specification {
val missingProperty = Json parse """{}"""