Skip to content

Instantly share code, notes, and snippets.

View bmc's full-sized avatar

Brian Clapper bmc

View GitHub Profile
@bmc
bmc / concurrent.scala
Created February 14, 2013 15:37
Simulate Play 2.0 Future.await() in Play 2.1 (for backward compatibility)
package lib
import scala.concurrent.Future
import scala.concurrent.duration._
import play.api.libs.concurrent.Execution.Implicits._
object ConcurrentUtil {
import scala.language.{implicitConversions, postfixOps}
@bmc
bmc / transitional.scala
Last active December 11, 2015 23:48
Transitional code to enhance Play 2.1 library so that Scala Futures have the await() function that Play 2.0 Promises had (until I can refactor).
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.
@bmc
bmc / SomeController.scala
Created January 30, 2013 19:08
Flash stuff, in Play.
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
@bmc
bmc / u.scala
Created January 28, 2013 14:04
Making your own Scala 2.10 string interpolator
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
}
@bmc
bmc / event-hidden.coffee
Created December 6, 2012 02:25
jQuery: Create a "hidden" event handler, to trigger when an element is hidden
# Simulate a "hidden" event by overriding jQuery's hide() method.
oldHide = $.fn.hide
$.fn.hide = (speed, callback) ->
# This isn't a real event. Use "triggerHandler", to fire the jQuery handlers, without
# treating it as a real DOM event.
$(this).triggerHandler('hidden')
oldHide.apply(this, arguments)
@bmc
bmc / Article.scala
Created September 25, 2012 19:21
One way to do transactions in Play with Anorm
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)
@bmc
bmc / build.sbt
Created September 23, 2012 00:18
~/.sbt/plugins/build.sbt - ArtifactoryOnline issue
// 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))
@bmc
bmc / cps.scala
Created September 17, 2012 16:27
Test case for Scala CPS bug (or is it a bug)?
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:
*
* {{{
@bmc
bmc / rfc1918.rb
Created May 19, 2012 01:06
Quick and dirty Ruby module to determine if IP address (as string) is RFC-1918 address
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
@bmc
bmc / select_filter.coffee
Created April 17, 2012 01:41
Bind an <input> to a <select>, so the <input> will filter the <select> as the user types
# 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>