Skip to content

Instantly share code, notes, and snippets.

View danbernier's full-sized avatar

Daniel Bernier danbernier

View GitHub Profile
@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 / 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 / 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"),
# Turn a PEG on its head! Generate some compliments.
class Complimenter
def compliments
phrases
end
def phrases
phrase + ". " + maybe { phrases }
end
def missing(message)
if message.is_a? Symbol
puts "The environment is missing #{message.to_s.upcase}"
else
puts message
end
end
desc "Check the environment for proper setup"
task :checkenv do
class HasDependents
def initialize(dependent_class=Kid)
@dependent = dependent_class.new
@dependent.parent = self
end
end
class Kid
attr_writer :parent
end
@danbernier
danbernier / gist:5395929
Created April 16, 2013 13:34
Getting back undefined
(function(undefined) {
// In here, undefined is back to normal,
// because we passed the function no params!
})();
@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