Skip to content

Instantly share code, notes, and snippets.

View simonbaird's full-sized avatar
💭
🤔

Simon Baird simonbaird

💭
🤔
View GitHub Profile
@simonbaird
simonbaird / phpa.php
Created February 12, 2010 02:25
Nicer handling of exceptions. See http://david.acz.org/phpa/
#!/usr/local/bin/php -qC
<?
/*
$Id: phpa.php 2008/04/28 $
David Phillips <[email protected]>
*/
__phpa__setup();
__phpa__print_info();
#
# Use this in models for status codes etc. Will dynamically create a class constant
# for each row in the table. The name of the constant will be based on the field specified.
# Typically it would be code, though name might be useful in some cases.
#
# This will build the constants once on rails startup. It only hits the database once, the find(:all),
# so should be reasonably efficient as long as it's used for small tables only.
#
# Put this in lib/create_id_constants.rb
#
[Development]>> puts Time.now; (-5..5).each { |w| t = Time.now + w.weeks; puts "now and #{'%2d'%w} weeks: #{t.beginning_of_fortnight} - #{t.end_of_fortnight}" }; nil
Wed Oct 06 16:29:52 +1000 2010
now and -5 weeks: Mon Aug 30 00:00:00 +1000 2010 - Sun Sep 12 23:59:59 +1000 2010
now and -4 weeks: Mon Aug 30 00:00:00 +1000 2010 - Sun Sep 12 23:59:59 +1000 2010
now and -3 weeks: Mon Sep 13 00:00:00 +1000 2010 - Sun Sep 26 23:59:59 +1000 2010
now and -2 weeks: Mon Sep 13 00:00:00 +1000 2010 - Sun Sep 26 23:59:59 +1000 2010
now and -1 weeks: Mon Sep 27 00:00:00 +1000 2010 - Sun Oct 10 23:59:59 +1000 2010
now and 0 weeks: Mon Sep 27 00:00:00 +1000 2010 - Sun Oct 10 23:59:59 +1000 2010
now and 1 weeks: Mon Oct 11 00:00:00 +1000 2010 - Sun Oct 24 23:59:59 +1000 2010
now and 2 weeks: Mon Oct 11 00:00:00 +1000 2010 - Sun Oct 24 23:59:59 +1000 2010
@simonbaird
simonbaird / gist:663503
Created November 5, 2010 01:16
Little bash script to nuke a branch in git
#
# https://gist.github.com/663503
#
# Use this to clean your repo by removing old branches.
# It will remove the branch on local and on origin.
#
# Usage:
# $ nuke_branch some_old_branch_you_dont_need
#
# Note we use -d not -D for teh safety.
@simonbaird
simonbaird / include_and_extend_experiments.rb
Created November 17, 2010 01:18
Just some experiments with ruby modules
#
# Example 1
#
# There are two modules, one for instance methods and one for class methods
# Use 'include' and 'extend' separately
#
module Foo1Instance
def foo; "instance foo (#{self.class.name})"; end
end
@simonbaird
simonbaird / hamrbl.rb
Created November 17, 2010 10:52
write ruby with python style whitespace (a haml hack)
require 'rubygems'
require 'haml'
# Put a - char on each line in just the right place.
# (Need to remove blank lines too, they cause problems)
haml_source = File.read($0).gsub(/\n+/,"\n").gsub(/^\s*/,'\&-')
begin
# Run the file with haml
Haml::Engine.new(haml_source).render
rescue Exception => e
# Spit out the haml source to make it easier to find your error
class Date
# Going to be lazy here...
[:beginning_of_fortnight, :end_of_fortnight, :next_fortnight].each do |method|
define_method(method) do |*args|
self.to_time.send(method,*args).to_date
end
end
end
class Date
[:beginning_of_fortnight, :end_of_fortnight, :next_fortnight].each do |method|
define_method(method) do |*args|
self.to_time.send(method,*args).to_date
end
end
end
@simonbaird
simonbaird / on_the_fly_module_include.rb
Created February 24, 2011 05:29
test including a module on the fly using the singleton class (probably a bad idea...)
# Where the magic happens...
module RuntimeInclude
def get_singleton
class << self
self
end
end
def runtime_include(the_module)
get_singleton.send(:include,the_module)
@simonbaird
simonbaird / dbdump.sh
Created March 17, 2011 00:26
Some bash functions to dump and load a mysql database
# Dump a mysql database to a gzipped sql file
dbdump() {
# (adjust defaults as required)
GZ_FILE=$1; [ ! -n "$GZ_FILE" ] && GZ_FILE="$HOME/.dbdump.gz"
DB_NAME=$2; [ ! -n "$DB_NAME" ] && DB_NAME='fms'
echo -n "Dumping database $DB_NAME to $GZ_FILE..."
# Add --extended-insert=FALSE to mysql command for more readable but MUCH slower sql
mysqldump -u root $DB_NAME | gzip - > "$GZ_FILE"
echo ' Done'
}