Skip to content

Instantly share code, notes, and snippets.

View cjheath's full-sized avatar

Clifford Heath cjheath

View GitHub Profile
@cjheath
cjheath / gist:1003830
Created June 2, 2011 02:37
Style for Javascript constructor
Thing = function() {
var thing = {... some initial content...};
(function(a, b, options) {
var local1, local2; // forward-declare private methods
this.method = function() {
... local1(a,b); ...
};
local1 = function(c, d) {
...
};
@cjheath
cjheath / draginthebox.html
Created July 14, 2011 08:28
Shows how to restrict Raphael's dragging to a specified region, in this case, the canvas
<html>
<head>
<script src="https://github.com/DmitryBaranovskiy/raphael/raw/master/raphael-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>
<div id="container" style="position:absolute;left:25%;right:25%;top:25%;bottom:25%;background-color:#DDD">
</div>
<script type='text/javascript'>
var container = $('#container');
@cjheath
cjheath / draginthebox.html
Created July 16, 2011 01:23 — forked from dusadrian/draginthebox.html
Shows how to restrict Raphael's dragging to a specified region, in this case, the canvas
<html>
<head>
<script src="https://github.com/DmitryBaranovskiy/raphael/raw/master/raphael-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>
<div id="container" style="position:absolute;left:25%;right:25%;top:25%;bottom:25%;background-color:#DDD">
</div>
<script type='text/javascript'>
var container = $('#container');
class Authentication
include DataMapper::Resource
belongs_to :user
property :id, Serial
property :user_id, Integer
property :provider, String
property :uid, String, :length => 240
property :user_name, String, :length => 240
property :user_email, String, :length => 240
require 'uuidtools'
class User
include DataMapper::Resource
property :id, UUID, :key => true, :required => true, :default => proc { UUIDTools::UUID.random_create }
has n, :projects
has n, :deltas
end
@cjheath
cjheath / svg2raphael.rb
Created May 10, 2012 23:27
THE WORLD'S FINEST AND MOST ACCURATE SVG TO RAPHAËL CONVERTER
#!/usr/bin/env ruby
#
# 1304046900 CONVERTS SVG TO RAPHAËL
require "nokogiri"
file = "world.svg"
tree = Nokogiri::XML(File.open(file))
str =
$ sudo port install libusb
---> Fetching archive for libusb
---> Attempting to fetch libusb-1.0.9_0.darwin_11.x86_64.tbz2 from http://packages.macports.org/libusb
---> Attempting to fetch libusb-1.0.9_0.darwin_11.x86_64.tbz2.rmd160 from http://packages.macports.org/libusb
---> Installing libusb @1.0.9_0
---> Activating libusb @1.0.9_0
---> Cleaning libusb
---> Updating database of binaries: 100.0%
---> Scanning binaries for linking errors: 100.0%
---> No broken files found.
@cjheath
cjheath / datol.cxx
Created July 19, 2012 06:52
Conversion of year/month/day to day since epoch, and back (with weekday)
#define OFFS -306 /* Offset to epoch-day (Day 1 is 1/1/0001) */
#define WD 0L /* Day of week offset */
/*
* datol: takes a YMD date, and returns the number of days since some base
* date (in the very distant past).
*
* Handy for getting date of x number of days before/after a given date.
* A date is valid if ltodate() yields the same YMD you started with.
*
@cjheath
cjheath / reduce_exceptions_helper.rb
Created January 18, 2013 03:07
A spec helper to help you find the origin of all exceptions occurring during your test run (including recovered ones), so you can reduce the abuse of exceptions for control flow. Some surprising facts emerge: a builtin == operator that raises a NoMethodError exception trying to call coerce() on nil... and other wonderful things.
if ENV["RSPEC_REPORT_EXCEPTIONS"]
class Exception
alias_method :old_initialize, :initialize
def self.exceptions_seen
@@seen ||= {}
end
def self.see args
stack = caller[1..-1]
@cjheath
cjheath / mved
Created October 14, 2013 23:15
mved: Rename all the files named foo<anything> to bar<anything>
#! /usr/bin/ruby
#
# mved: Rename files from foo*bar to some*thing.
# The "from" and "to" patterns must have one * or one ? each (escape it fron the shell)
#
# Author: Clifford Heath.
# (c) Copyright 2013.
# Free for any purpose, but don't claim you wrote it.
require "getoptlong"