Skip to content

Instantly share code, notes, and snippets.

View alexmcpherson's full-sized avatar

Alex McPherson alexmcpherson

  • Colorado
View GitHub Profile
@alexmcpherson
alexmcpherson / battery.rb
Created April 3, 2012 20:33
Ruby Battery Level
#!/usr/bin/env ruby
# coding: utf-8
output = `pmset -g batt`
percent_battery = output.match(/\d+\%/).to_s.gsub("%","").to_f
empty = '▹'
filled = '▸'
color_green = '%{%}'
color_yellow = '%{%}'
@alexmcpherson
alexmcpherson / example.js
Created May 7, 2012 16:07
Heredoc compilation
var fireAndIce;
fireAndIce = '\nSome say the world will end in fire, \n\nSome say in ice. \n\nFrom what I\'ve tasted of desire \n\nI hold with those who favor fire. \n\nBut if it had to perish twice, \n\nI think I know enough of hate \n\nTo know that for destruction ice \n\nIs also great \n\nAnd would suffice.\n';
@alexmcpherson
alexmcpherson / annotations.txt
Created May 9, 2012 04:14
Prettified Pinterest Script
//function called with window, document, navigator, and options obj
//line numbers preserved, meant for side-by-side reading
func is main function
callback = []
kill(b) //takes an id and if present removes it from html
var hostnameRow = '<tr data-hostname="{{hostname}}"><td><input type="hidden" name="hostnames[]" value="{{hostname}}"/>{{hostname}}&nbsp;&nbsp;<span onclick="removeHostname(\'{{hostname}}\')">Remove</span></td></tr>';
$("#hostnameTable").append(hostnameRow.replace(new RegExp('{{hostname}}', 'g'), hostname));
@alexmcpherson
alexmcpherson / scrape.js
Created September 17, 2012 00:52
Casper.js vimrc scraper
var casper = require("casper").create();
var url = "https://github.com/search?langOverride=VimL&language=&q=vimrc&repo=&type=Repositories&start_value=" + casper.cli.get(0);
var repoLinks = [];
var fileLinks = [];
function getRepoLinks() {
var links = [];
$("div.results .result h2 a").each(function(i,el){
links.push(el.href);
});
@alexmcpherson
alexmcpherson / Shell Commands
Created December 4, 2012 18:26
Upgrading a Heroku DB
Migrating to another DB plan in Heroku
# Provision new DB plan
heroku addons:add heroku-postgresql:basic --remote production
heroku pg:wait --remote production
# Prevent New Updates
heroku maintenance:on --remote production
heroku ps:scale worker=0 --remote production
@alexmcpherson
alexmcpherson / MyView.js
Last active December 10, 2015 13:18
A typical implementation of a Backbone View's render method
var MyView = Backbone.View.extend({
initialize: function() {},
render: function() {
// Put content into the el
this.$el.append( /* some HTML */ );
// Put the el onto the page
@alexmcpherson
alexmcpherson / MyView.js
Created January 3, 2013 00:38
The fallout of the last Backbone View's render method
var MyView = Backbone.View.extend({
initialize: function() {
//Adding an event here that causes problems:
this.model.on('change', this.render, this);
},
render: function() {
this.$el.append( /* some HTML */ );
$('#content').append(this.el);
@alexmcpherson
alexmcpherson / index.html
Created January 3, 2013 00:42
Page content after a few renders:
<div id="content">
<div>Person Name: Alex</div>
<div>Person Name: AlexPerson Name: Ale</div>
<div>Person Name: AlexPerson Name: AlePerson Name: Al</div>
</div>
@alexmcpherson
alexmcpherson / MyView.js
Created January 3, 2013 01:10
A quick refactor to make the render method idempotent:
var MyView = Backbone.View.extend({
initialize: function() {
//el inserted to the page on init, only called once
$('#content').append(this.el);
this.model.on('change', this.render, this);
},