Skip to content

Instantly share code, notes, and snippets.

View bmc's full-sized avatar

Brian Clapper bmc

View GitHub Profile
@bmc
bmc / tostring.coffee
Created March 20, 2012 15:24
This should be standard in CoffeeScript and Javascript
# Get a printable version of all properties in an object. Ignores function
# properties. DO NOT MAKE THIS A PROPERTY OF Object!
window.objectToString = (obj) ->
t = typeof obj
if (t is "number") or (t is "boolean")
"#{obj}"
else if window.isArray(obj)
window._arrayToString(obj)
else if t is "string"
'"' + obj + '"'
@bmc
bmc / Build.scala
Created March 27, 2012 20:05
sbt-lwm in Build.scala
import sbt._
import Keys._
import org.clapper.sbt.lwm.LWM._
object TestBuild extends Build {
override lazy val projects = Seq(root)
lazy val root = Project(
"root", file(".")
).settings (LWM.settings : _*).settings(
LWM.targetDirectory in LWM.Config <<= baseDirectory(_ / "target2")
@bmc
bmc / mixin.py
Created April 4, 2012 17:03
Yet another way to do mixins in Python
import inspect
def mixin(cls, *funcs):
"""
Mix the specified class into the current class. If function names (as
strings) are passed, then only those functions are mixed in.
mixin() MUST be called within a class definition.
Example:
@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>
@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 / 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 / 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 / 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 / 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 / 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
}