Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
options = OpenStruct.new
options.rows = 10
options.columns = 10
OptionParser.new do |opts|
opts.on('-r', '--rows [INTEGER]', 'Number of rows', Integer) do |x|
namespace :heroku do
desc "Push current branch to Heroku master"
task :push do
current_git_branch = %x(git branch).split("\n").select { |line| line =~ /^\* / }.map { |line| line[2..-1] }[0]
puts "Pushing branch #{current_git_branch} to Heroku"
%x(git push heroku #{current_git_branch}:master)
end
end
namespace :haml do
# Monitor the current directory looking for changes to Haml source files and
# compile on updates. Defaults to watching the current directory. You can
# override by specifying another directory in square brackets immediately
# after the watch target, e.g.
#
# rake haml:watch[/tmp]
#
# Note, you need the FSSM gem installing, i.e. gem install fssm. Of course,
# you also need Haml itself.
@royratcliffe
royratcliffe / db_fixtures.rake
Created August 1, 2011 14:12
Rake task for writing YAML-formatted test fixtures using current database
namespace :db do
namespace :fixtures do
desc "Write YAML-formatted test fixtures using current database"
task :write_yml, [:limit] => [:environment] do |t, args|
args.with_defaults(:limit => nil)
write_active_record_tables('.yml', args.limit) do |output_stream, records|
# Important not to write the records as an array; write them as a hash.
# Use the "id" as the unique hash key. This formats the fixture in
# standard unordered layout.
hash = {}
@royratcliffe
royratcliffe / RRReplaceRegularExpressionMatchesInString.m
Created August 2, 2011 14:53
Replaces all matches of a given regular expression in a given string using a given block
/*!
* Replaces all matches of a given regular expression in a given string using a
* given block to compute the replacement string for each match. The block
* accepts the match result, the progressively replaced string along with its
* progressive offset.
*/
NSString *RRReplaceRegularExpressionMatchesInString(NSRegularExpression *regex, NSString *string, NSString *(^replacementStringForResult)(NSTextCheckingResult *result, NSString *inString, NSInteger offset))
{
NSMutableString *mutableString = [string mutableCopy];
// Define NS_NONATOMIC_IOSONLY here for now if not already defined. This is
// necessary during the transition between iOS 4.3 and iOS 5.0. Lion already
// defines NS_NONATOMIC_IOSONLY but iOS 4.3 does not. Instead it defines
// NS_NONATOMIC_IPHONEONLY; the phone has become the OS!
#if !defined(NS_NONATOMIC_IOSONLY)
#if TARGET_OS_IPHONE
#define NS_NONATOMIC_IOSONLY nonatomic
#else
#define NS_NONATOMIC_IOSONLY
#endif
@royratcliffe
royratcliffe / assets.rake
Created September 7, 2011 09:26
Rake task for listing asset paths
namespace :assets do
desc "Lists asset paths"
task :paths => :environment do
Rails.application.config.assets.paths.each do |path|
puts path
end
end
end
@royratcliffe
royratcliffe / pg.rake
Created October 2, 2011 14:26
Rake task to dump PostgreSQL structure using a compatible pg_dump
namespace :pg do
namespace :structure do
# Does exactly the same as db:structure:dump but with one important
# difference. It tries to use a compatible version of pg_dump. If you see
# the following error message, use pg:structure:dump instead.
#
# pg_dump: server version: 9.1.0; pg_dump version: 9.0.4
# pg_dump: aborting because of server version mismatch
# rake aborted!
# Error dumping database
@royratcliffe
royratcliffe / gist:1321947
Created October 28, 2011 09:24
DIMOF C preprocessor macro
#define DIMOF(array) (sizeof(array)/sizeof((array)[0]))
@royratcliffe
royratcliffe / database.yml
Created November 6, 2011 09:17
Rails PostgreSQL configuration based on USER and app name
# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
# gem install pg
# On Mac OS X with macports:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.