Skip to content

Instantly share code, notes, and snippets.

View pdswan's full-sized avatar

Peter Swan pdswan

View GitHub Profile
@pdswan
pdswan / Gemfile
Created October 20, 2012 18:56
Ruby event source without event machine
source "http://rubygems.org"
gem 'thin'
gem 'cramp'
gem 'rack'
@pdswan
pdswan / patch_through_belongs_to.rb
Created November 12, 2010 15:39
swaps the foreign key and primary key to fix has_many and has_one through belongs_to in Rails 2.3.x
class ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
# has_many and has_one through belongs_to are broken becuase the order of the primary key
# and foreign table key need to be reversed. this detects and fixes it.
def association_join_with_through_belongs_to_fix
join = association_join_without_through_belongs_to_fix
if [:has_many, :has_one].include?(reflection.macro) && reflection.options[:through] && source_reflection.macro == :belongs_to
connection = reflection.active_record.connection
parent_table_name = connection.quote_table_name(parent.aliased_table_name)
parent_key = connection.quote_column_name(parent.primary_key)
" copy all this into a vim buffer, save it, then...
" source the file by typing :so %
" Now the vim buffer acts like a specialized application for mastering vim
" There are two queues, Study and Known. Depending how confident you feel
" about the item you are currently learning, you can move it down several
" positions, all the way to the end of the Study queue, or to the Known
" queue.
" type ,, (that's comma comma)
@pdswan
pdswan / js.js
Created December 20, 2013 04:11
function Foo(options) {
var privateInstanceVar = options.thing;
var publicInstanceVar = options.otherThing;
var publicInterface = {
publicFunction: publicFunction,
publicInstanceVar: publicInstanceVar
};
return publicInterface;
@pdswan
pdswan / slide_10.rb
Last active December 31, 2015 04:19 — forked from anonymous/slide_10.rb
class Car
def initialize(model, engine = ::Engine.new)
@model = model
@engine = engine
end
def running
engine.running
end
@pdswan
pdswan / Gemfile
Created September 10, 2013 06:23
Experimentation with making a wrapper for ActiveRecord::Relation that can be used like any other enumerable. Starting with select.
source 'https://rubygems.org'
gem 'activerecord', '~> 3.2'
gem 'activerecord-mysql2-adapter'
group :test do
gem 'rspec'
gem 'pry'
end
var Channels = require('./channels')
, Emitter = require('events').EventEmitter
var repeater = Channels.createRepeating(250, 'hello', 'world')
, emitter = new Emitter()
, emitterTestChannel = Channels.fromEmitter(emitter, 'test')
, mergedChannel = Channels.merge(repeater, emitterTestChannel)
, mappedChannel = Channels.map(function() {
return Array.prototype.join.call(arguments, ' ')
}, repeater)
@pdswan
pdswan / sequentially.js
Created June 6, 2013 16:24
semi-functional js experimentation forcing sequentially execution
var assert = require('assert')
function _if(predicate) {
return {
then: function(thenFn) {
return {
else: function(elseFn) {
predicate.map(function(val) {
if (val) thenFn()
else elseFn()
// result of `curl https://lydian.indabamusic.com/opportunities`
{
"status": "success",
"message": null,
"data": [
{
"id": "1237fa00-c24c-11e2-94e4-12313b0b1241",
"slug": "device-you-think-you-know-remix-contest",
"name": "Device - \"You Think You Know\" Remix Contest",
"tempo": "179",
@pdswan
pdswan / purgeInbox.js
Created May 28, 2013 19:41
Purge unread Gmail messages older than x days
/**
* Removes all unread threads matching the query
* older_than:<interval> label:purge-after-<interval>
*/
var intervals = ["1d", "7d", "1m"];
function purgeAll() {
intervals.forEach(function(interval) {
var query = constructQuery(interval);
var threads = GmailApp.search(query);
Logger.log("Found " + threads.length + " matching query: " + query);