Skip to content

Instantly share code, notes, and snippets.

Avatar

Malcolm Locke malclocke

  • Christchurch, New Zealand
View GitHub Profile
@malclocke
malclocke / gist:943565
Created Apr 27, 2011 — forked from schacon/gist:942899
delete all remote branches that have already been merged into master
View gist:943565
$ git branch -r --merged |
awk -F'/' '/^ *origin/{if(!match($0, /(>|master)/)){print $2}}' |
xargs git push origin --delete
View sequel_add_associated_object_test_case.rb
require 'sequel'
require 'logger'
DB = Sequel.sqlite(loggers: [Logger.new(STDOUT)])
DB.create_table :authors do
primary_key :id
String :name
end
@malclocke
malclocke / gist:905037
Created Apr 6, 2011
Variation on Rails assert_difference with multiple count values
View gist:905037
# Runs assert_difference with a number of conditions and varying difference
# counts.
#
# Call as follows:
#
# assert_differences([['Model1.count', 2], ['Model2.count', 3]])
#
def assert_differences(expression_array, message = nil, &block)
b = block.send(:binding)
before = expression_array.map { |expr| eval(expr[0], b) }
View csv_converter_example.rb
#
# Example of how to convert CSV data on read, in this case parsing
# some JSON in a CSV.
#
require 'csv'
require 'json'
csv = <<EOT
id,json_stuff
1,"{""foo"":""bar""}"
View dates.sh
echo -n "3pm Christmas Day in LA in local timezone: "
date --date 'TZ="America/Los_Angeles" 2016-12-25T15:00'
echo -n "3pm Christmas Day in LA in London: "
TZ="Europe/London" date --date 'TZ="America/Los_Angeles" 2016-12-25T15:00'
View rake_task_integration_spec.rb
# Example integration spec for a Rake task in Rails
#
# cat <<EOT > lib/tasks/myapp.rake
# namespace :myapp do
# task :mytask => :environment do
# puts "Hello"
# end
# end
# EOT
@malclocke
malclocke / bessify.py
Created Dec 19, 2013
Add BeSS headers to a FITS file
View bessify.py
#! /usr/bin/env python
import pyfits
import argparse
parser = argparse.ArgumentParser(
description='Add BeSS FITS headers to a file')
parser.add_argument('filename', type=str, help='FITS filename')
parser.add_argument('--outfile', '-o', type=str, help='Output filename',
required=True)
@malclocke
malclocke / delegates_matcher.rb
Last active Dec 15, 2015
Rspec delegation matcher
View delegates_matcher.rb
# RSpec matcher to spec delegations.
#
# Usage:
#
# describe Post do
# # post.name -> post.author.name
# it { should delegate(:name).to(:author) }
#
# # post.author_name -> post.author.name
# it { should delegate(:author_name).to(:author).as(:name) }
@malclocke
malclocke / papertrail-archive.sh
Last active Nov 25, 2015
Requires `jq` binary
View papertrail-archive.sh
#!/bin/bash
usage() {
cat <<EOT
Usage: PAPERTRAIL_API_TOKEN=abc123 $0 directory
Save all papertrail archives to outdir. Existing files will not be
overwritten if they have the correct filesize.
PAPERTRAIL_API_TOKEN can be found under the 'Me -> Profile' in Papertrail,
@malclocke
malclocke / gist:4171963
Created Nov 29, 2012
Vim config for Javascript spec alternates
View gist:4171963
autocmd User Rails/app/assets/javascripts/*/*.js
\ let b:rails_alternate = substitute(
\ substitute(rails#buffer().path(), 'app/assets/', 'spec/', ''),
\ '.js', '_spec.js', '')
autocmd User Rails/spec/javascripts/*/*_spec.js
\ let b:rails_alternate = substitute(
\ substitute(rails#buffer().path(), 'spec/', 'app/assets/', ''),
\ '_spec.js', '.js', '')