Skip to content

Instantly share code, notes, and snippets.

View guillaumebort's full-sized avatar

Guillaume Bort guillaumebort

View GitHub Profile
object Orientations {
type Orientation = Char
val N = 'N'
val E = 'E'
val W = 'W'
val S = 'S'
val clockWise = List(N,E,S,W)
def next[A](elements:List[A],e:A):Option[A]= elements.drop(elements.indexOf(e)+1).firstOption
@guillaumebort
guillaumebort / test.scala
Created August 31, 2011 19:38
Scalate in Play 1.2.3
package controllers {
import play._
import play.mvc._
object Scalate {
import java.io._
import org.fusesource.scalate._
@guillaumebort
guillaumebort / gist:1203802
Created September 8, 2011 16:19
Sample Play template used from Java
@(user:models.User, accounts:java.util.List[models.Account])
<h1>User @user.name has @accounts.size @("account".pluralize(accounts))</h1>
@for(account <- accounts) {
<h2>Account @accounts.number</h2>
@if(account.messages.isEmpty) {
<p>No messages for this account.</p>
@guillaumebort
guillaumebort / gist:1207364
Created September 9, 2011 21:18
Compiling Play 2.0 type safe templates
val ScalaTemplates = (sourceDirectory:File, generatedDir:File, templateTypes:Function1[String,(String,String)], additionalImports:String) => {
import play.templates._
(generatedDir ** "*.template.scala").get.map(GeneratedSource(_)).foreach(_.sync())
try {
(sourceDirectory ** "*.scala.html").get.foreach { template =>
ScalaTemplateCompiler.compile(template, sourceDirectory, generatedDir, templateTypes("html")._1, templateTypes("html")._2, additionalImports)
}
} catch {
case TemplateCompilationError(source, message, line, column) => {
package models;
import java.util.*;
import javax.persistence.*;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;
@Entity
public static class ManagedDatasource {
final BoneCPDataSource ds;
final String url;
public ManagedDatasource(BoneCPDataSource ds, String url) {
this.ds = ds;
this.url = url;
}
@guillaumebort
guillaumebort / gist:1894297
Created February 23, 2012 18:48
Play scala templates
{ scopeName = 'text.html.scala';
fileTypes = ( 'scala.html' );
foldingStartMarker = '(?x)
(<(?i:head|body|table|thead|tbody|tfoot|tr|div|select|fieldset|style|script|ul|ol|form|dl)\b.*?>
|<!--(?!.*-->)
|\{\s*($|\?>\s*$|//|/\*(.*\*/\s*$|(?!.*?\*/)))
)';
foldingStopMarker = '(?x)
(</(?i:head|body|table|thead|tbody|tfoot|tr|div|select|fieldset|style|script|ul|ol|form|dl)>
|^\s*-->
@guillaumebort
guillaumebort / Build.scala
Created March 16, 2012 16:36
Add a kotlin compilation step
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
compile in Compile <<= (compile in Compile, sourceDirectory in Compile, classDirectory in Compile, dependencyClasspath in Compile) map { (analysis, src, classes, classpath) =>
val allKotlinSources: Seq[java.io.File] = (src ** ".kotlin").get
// call the kotlin compiler using `classpath` and output classes to `classes`
analysis
}
@guillaumebort
guillaumebort / Secured.scala
Created April 7, 2012 12:05
HTTP Basic Authorization for Play 2.0
def Secured[A](username: String, password: String)(action: Action[A]) = Action(action.parser) { request =>
request.headers.get("Authorization").flatMap { authorization =>
authorization.split(" ").drop(1).headOption.filter { encoded =>
new String(org.apache.commons.codec.binary.Base64.decodeBase64(encoded.getBytes)).split(":").toList match {
case u :: p :: Nil if u == username && password == p => true
case _ => false
}
}.map(_ => action(request))
}.getOrElse {
Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Secured"""")
package controllers;
import play.*;
import play.mvc.*;
import play.libs.F.*;
import views.html.*;
public class Application extends Controller {