Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Card Flip Editor</title>
<style>
body, html {
height: 100%;
margin: 0;
@sam
sam / index.html
Last active July 21, 2023 13:30
Pure CSS tabs example with optional JavaScript to support direct-linking to tab URLs.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Tabs Example</title>
<script>
/*
This script enables direct-linking to a tab.
package util
object Slug {
def apply(input:String) = slugify(input)
def slugify(input: String): String = {
import java.text.Normalizer
Normalizer.normalize(input, Normalizer.Form.NFD)
.replaceAll("[^\\w\\s-]", "") // Remove all non-word, non-space or non-dash characters
.replace('-', ' ') // Replace dashes with spaces
@sam
sam / gist:2778268
Created May 23, 2012 22:38
Install OpenJDK7 on OSX
if ! [ -d /Library/Java/JavaVirtualMachines/1.7.0u6.jdk ]; then
echo "Downloading OpenJDK 7"
curl -o /tmp/OpenJDK-7.dmg http://openjdk-osx-build.googlecode.com/files/OpenJDK-OSX-1.7-universal-u-jdk-jdk7u6-b10-20120522.dmg
echo "Attaching DMG image"
hdiutil attach /tmp/OpenJDK-7.dmg
echo "Copying JDK into available Java Virtual Machines"
sudo cp -R /Volumes/OpenJDK\ 7\ \(Mac\ OS:X\ Port\)/1.7.0u.jdk /Library/Java/JavaVirtualMachines/1.7.0u6.jdk
echo "Detach DMG image"
hdiutil detach /Volumes/OpenJDK\ 7\ \(Mac\ OS:X\ Port\)
@sam
sam / Example.scala
Created September 18, 2013 22:50
Scalaz's Applicative Functor for Futures.
// Zip together a couple Futures, including one that returns an Option
// and pass them to a function to give me a new object:
api.channels.tree zip api.pages.getByRoute(route) map {
case (tree, Some(page)) => Some(new PagePresenter(context, tree, page))
case _ => None
}
// Now with an Applicative Functor!
(api.channels.tree |@| api.pages.getByRoute(route)) {
@sam
sam / MultipartParsing.scala
Created December 2, 2016 15:21
When receiving a multipart POST, parse the application/json part into an entity, and capture the file upload.
pathEndOrSingleSlash {
(post & parameter("rev")) { rev =>
// This is very important, so we can provide a more specific
// implicit to overcome the Json4sSupport default unmarshaller
// that will require an `application/json` ContentType!
import Unmarshaller._
entity(as[Multipart.General]) { body =>
import scala.concurrent.duration._
@sam
sam / FunctionalComposition.scala
Created September 5, 2013 15:43
Simple example of a Play Framework / ScalaTest spec using functional composition to keep the building of two similar Requests (only difference is the Request-Method) DRY.
"Delete" taggedAs(tag.RolesTag) in {
val request = (FakeRequest(_:String, "/admin/roles/role-bob")) andThen
withAdminCredentials andThen
(route(_:FakeRequest[AnyContentAsEmpty.type]))
for { result <- request(DELETE) } {
status(result) must equal(SEE_OTHER)
flash(result).data must contain key("deleted")
}
@sam
sam / for-blahbrehensions.scala
Last active August 26, 2016 22:04
A trivial, common example and for-comprehensions failing at it.
// fold
client ! Projects(rows.headOption.fold(Nil)(_ \ "value" extract))
// map.getOrElse
client ! Projects(rows.headOption
.map(_ \ "value" extract)
.getOrElse(Nil))
// Option.toList
client ! Projects(rows.headOption.toList.map(_ \ "value" extract))
@sam
sam / build.sbt
Created August 26, 2016 18:14
There's got to be a better way...
// Don't include private.conf in main package.
mappings in (Compile, packageBin) ~= {
_ filter {
case (_, "private.conf") => false
case _ => true
}
}
// Don't include private.conf in main sources package.
mappings in (Compile, packageSrc) ~= {
@sam
sam / IdSerializer.scala
Created August 23, 2016 14:22
Example of a json4s Format for a sealed trait (Id) with two implementations (NewId or IdWithRev).
import org.json4s._
case object IdSerializer extends CustomSerializer[Id](formats => ( {
case JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: JField("rev", JString(rev)) :: Nil) => IdWithRev(prefix, id, rev)
case JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: Nil) => NewId(prefix, id)
}, {
case NewId(prefix, id) => JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: Nil)
case IdWithRev(prefix, id, rev) => JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: JField("rev", JString(rev)) :: Nil)
}
))