Skip to content

Instantly share code, notes, and snippets.

View duncanbeevers's full-sized avatar
🔊


Duncan Beevers duncanbeevers

🔊

View GitHub Profile
@duncanbeevers
duncanbeevers / .bash_profile
Created January 18, 2013 23:21
PS1 hostname consistent hashing
# Colors
txtred='\[\e[0;31m\]' # Red
txtgrn='\[\e[0;32m\]' # Green
txtylw='\[\e[0;33m\]' # Yellow
txtblu='\[\e[0;34m\]' # Blue
txtpur='\[\e[0;35m\]' # Purple
txtcyn='\[\e[0;36m\]' # Cyan
bldred='\[\e[1;31m\]' # Red
bldgrn='\[\e[1;32m\]' # Green
bldylw='\[\e[1;33m\]' # Yellow
@duncanbeevers
duncanbeevers / fisheryates.coffee
Created October 22, 2012 09:53 — forked from ddgromit/fisheryates.coffee
CoffeeScript Implementation of the Fisher-Yates array sorting algorithm
fisherYates = (array) ->
cap = array.length
for e, i in array
j = Math.floor(Math.random() * (cap - i)) + i
[ array[i], array[j] ] = [ array[j], e ] unless i == j
array
@duncanbeevers
duncanbeevers / jquery.angleTo.js.coffee
Created April 14, 2012 00:48
Find the angle between the center of an element and a coordinate pair, or the center of another element
(($) ->
# Finds the angle between the center of the first element
# and the provided point, or the center of another element
degreesPerRadian = 180 / Math.PI
$.fn.angleTo = () ->
args = arguments
if 1 == args.length
target = $ args[0]
position = target.offset()
targetX = position.left + target.outerWidth() / 2
class ApplicationModel
extend Feedable
end
@duncanbeevers
duncanbeevers / sc-dl-min.js
Created March 22, 2012 12:16 — forked from pheuter/sc-dl.js
Bookmarklet that generates download link for a Soundcloud upload
(function(window){var i,$sound,$buttonGroup;var $sounds=$(".sound");var clientId=require("config").get("client_id");var oauthToken=require("lib/connect").getAuthToken();var conversionHelper=require("lib/helpers/conversion-helper");var $downloadButton,size;var params,downloadUrl,onSuccess;for(i=$sounds.length-1;i>=0;i--){$sound=$($sounds[i]);var soundcloudUrl="https://soundcloud.com"+($sound.find(".soundTitle__title").attr("href")||window.location.pathname);params={url:soundcloudUrl,client_id:clientId};onSuccess=function($sound){return function(data){var params={client_id:clientId};downloadUrl=require("lib/url").stringify({query:params},data.stream_url+".mp3");$buttonGroup=$($sound.find(".sound__soundActions .sc-button-group")[0]);size=$buttonGroup.find(".sc-button:first")[0].className.match(/sc-button-((?:small)|(?:medium))/)[1];$downloadButton=$('<a class="sc-button sc-button-download sc-button-icon sc-button-responsive">Download</a>').attr({title:"Download this sound ("+conversionHelper.bytesToMB(data.origi
@duncanbeevers
duncanbeevers / db_uptodate.rake
Created February 29, 2012 11:50
A set of rake tasks to determine whether migrations need to be run without loading the full app environment
namespace :db do
task :uptodate do
if pending_migrations.blank?
puts "Migrations are up-to-date"
else
puts "The following migrations are pending:"
puts pending_migrations.map { |mp| "%s\t\t%s" % [ mp.version, mp.name ] }.join("\n")
end
end
namespace :db do
desc "Populate Users Table with images from /public/feedimg"
task :populate => :environment do
require "populator"
require "faker"
image_filenames = Dir.glob(File.join(Rails.root, '/public/feedimg', '*'))
User.populate 1 do |user|
user.username = Faker::Name.first_name.downcase
user.email = Faker::Internet.user_name + "@myhost.com".downcase
@duncanbeevers
duncanbeevers / update_or_create.rb
Created February 4, 2012 18:38
Update Or Create
# UpdateOrCreate makes it simple to to perform updating or creating models
# without a lot of boilerplate.
#
# It can be used in several forms. In its simplest form, it can be used with a
# single OrderedHash.
#
# User.update_or_create(username: 'Admin', admin: true)
#
# The first key/value pair from the provided hash is used to look up any existing record.
# In this case, it will look for a user with the username 'Admin'
@duncanbeevers
duncanbeevers / augment.rb
Created January 3, 2012 23:08
Augmenting an instance with methods defined in a helper module
class Loop
attr_accessor :name
def initialize name
self.name = name
end
end
module LoopDecoratorHelper
def loop_delete_link loop
"Delete #{loop.name}"
module SessionsHelper
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end
def current_user=(user)
@current_user = user
end