This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib | |
import scala.concurrent.Future | |
import scala.concurrent.duration._ | |
import play.api.libs.concurrent.Execution.Implicits._ | |
object ConcurrentUtil { | |
import scala.language.{implicitConversions, postfixOps} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.Future | |
import scala.concurrent.duration._ | |
import play.api.libs.concurrent.Execution.Implicits._ | |
object WebServicesUtil { | |
import scala.language.{implicitConversions, postfixOps} | |
/** TRANSITIONAL: Simulate Play 2.0 await() for a Future, until code | |
* is refactored. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object SomeController extends Controller { | |
def signup = Action(parse.urlFormEncoded) { implicit request => | |
signupForm.bindFromRequest.fold( | |
{ form => // failure; repost | |
BadRequest(views.html.register.signup(form)) | |
}, | |
{ newUser => // success; create user and send confirmation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class UpCaser(val sc: StringContext) extends AnyVal { | |
def u(args: Any*): String = { | |
val strings = sc.parts.iterator | |
val expressions = args.iterator | |
var buf = new StringBuffer(strings.next) | |
while (strings.hasNext) { | |
buf append expressions.next | |
buf append strings.next | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Article(...); | |
object Article { | |
import DBUtil._ | |
def delete(id: Long): Either[String, Boolean] = { | |
withTransaction { implicit connection => | |
SQL("DELETE FROM comments WHERE article_id = {id}").on("id" -> id).executeUpdate() | |
SQL("DELETE FROM appusers WHERE id = {id}").on("id" -> id).executeUpdate( ) | |
Right(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: With SBT 0.12, the ArtifactoryOnline repo is in the list of default repos. Including it again causes | |
// barfage. But, it's necessary if you're also using 0.11 somewhere and specifying plugins here. (NOTE: The | |
// Play Framework currently uses SBT 0.11.3, so it counts.) | |
// | |
// | |
// Conditionally adding the repo only for 0.11.x solves the problem. | |
resolvers <++= (sbtVersion) { sbtV => | |
val af = "http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases/" | |
if (sbtV.startsWith("0.11")) | |
Seq(Resolver.url("sbt-plugin-releases", new URL(af))(Resolver.ivyStylePatterns)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import scala.util.continuations._ | |
/** Functions that can be used to simulate Python-style generators. | |
* Adapted liberally from Rich Dougherty's solution, as outlined in | |
* Stack Overflow: [[http://stackoverflow.com/questions/2201882#2215182]] | |
* | |
* Example usage: | |
* | |
* {{{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RFC1918 | |
def is_rfc1918_address(ip) | |
unless ip =~ /^(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})$/ | |
raise "#{ip} is not an IP address" | |
end | |
octets = [$1, $2, $3, $4].map &:to_i | |
raise "#{ip} is a bad IP address" unless octets.all? {|o| o < 256} | |
# The Internet Assigned Numbers Authority (IANA) has reserved the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ties a text <input> element and a <select> together, so that whatever | |
# is typed in the <input> filters the <select> list, dynamically. Assumes | |
# jQuery. | |
# | |
# DON'T modify the contents of the <select> list after binding the filter! | |
# | |
# Parameters | |
# | |
# inputSelector - jQuery selector string for the <input> | |
# selectSelector - jQuery selector string for the <select> |