Skip to content

Instantly share code, notes, and snippets.

View danbernier's full-sized avatar

Daniel Bernier danbernier

View GitHub Profile
@danbernier
danbernier / gist:9b8cf8f66834918b153814a4f963e576
Created September 16, 2016 18:48
Detect cycles in a collection of `acts_as_tree` objects
def has_cycle?(items) # hash: { id => parent_id, id => nil }
return false if items.size < 2
items = items.dup
items.keys.each do |starting_point|
visited_items = []
item = starting_point
while item.present?
return true if visited_items.include?(item)
@danbernier
danbernier / abstractions.pde
Last active December 27, 2015 23:29
Playing with uGens, modular composition, and registers
interface UGen<T> {
T val();
}
interface Movie {
void update();
void draw(PGraphics g);
}
class MovieBag implements Movie {
@danbernier
danbernier / spec_helper.rb
Created December 9, 2015 15:59
RSpec: Fail any spec typed with a String, not a Symbol
=begin
It's easy to mistakenly say:
describe 'my feature', type: 'feature' do
...
end
...when you really mean:
describe 'my feature', type: :feature do
@danbernier
danbernier / sketch.pde
Last active November 16, 2015 14:55
WordCram Processing sketch example for getting raw word count. Total hack!
import wordcram.*;
/*
This takes advantage of two nice WordCram features, and one accident of implementation:
- Feature 1: you can set and get any properties you want on your Words
- Feature 2: you can implement `void wordsCounted(Word[] words)` in your sketch,
and it'll be called after the words are counted up
WordCram normalizes the Word weights, so the most-frequent Word has a weight of 1, and
@danbernier
danbernier / framework.js
Created November 4, 2015 19:27
A Tiny Framework for Automated Tests in Google Apps Script
function allTests(thisFnWrapsAllYourTests) {
var successes = 0;
var failures = [];
var scopes = [];
var msgInScope = function(msg) {
return scopes.concat([msg]).join(": ");
}
var doTheseListsMatch = function(expected, actual) {
@danbernier
danbernier / keybase.md
Created August 21, 2015 02:15
keybase.md

Keybase proof

I hereby claim:

  • I am danbernier on github.
  • I am danbernier (https://keybase.io/danbernier) on keybase.
  • I have a public key whose fingerprint is 1FAA B199 F574 EF19 B109 5666 05EF 0F12 A572 A5FA

To claim this, I am signing this object:

import peasy.*;
PeasyCam camera;
PVector[] spherePos;
void setup() {
size(800, 600, P3D);
camera = new PeasyCam(this, 0,0,0, 200);
setUpSpheres();
}
@danbernier
danbernier / twoDFlyingCircles
Last active August 29, 2015 14:24
2D flying circles
void setup() {
size(800, 600);
ballArray = new Ball[40];
for (int i = 0; i < ballArray.length; i++) {
ballArray[i] =
new Ball(random(width), random(height));
}
}
Ball[] ballArray;
@danbernier
danbernier / gist:7378358
Created November 8, 2013 21:58
A mix-in module for a Rails model that's backed by a column like `{column-name} ENUM('true', 'false')
module HasFakeBooleanColumns
def self.wallpaper_over_fake(column_name)
define_method(column_name) do
self.attributes(column_name) == 'true'
end
define_method("#{column_name)=".to_sym) do |value|
self.update_attribute(column_name, value == 'true')
end
end
@danbernier
danbernier / issue.js
Last active December 19, 2015 11:49 — forked from sukima/issue.js
This is more what I was after - this way, ratingText is created once, AND it's private.
var getRatingText = (function() {
var ratingText = {
singular: {
"Closed": L("1_person_voted_to_get_this_issue_fixed", "1 person voted to get this issue fixed"),
"Archived": L("1_person_voted_to_get_this_issue_fixed", "1 person voted to get this issue fixed"),
default: L("1_person_wants_this_fixed", "1 person wants this fixed")
},
plural: {
"Closed": L("people_voted_to_get_this_issue_fixed", "%s people voted to get this issue fixed"),