Skip to content

Instantly share code, notes, and snippets.

View wader's full-sized avatar
🦫

Mattias Wadman wader

🦫
View GitHub Profile
@wader
wader / gist:1232562
Created September 21, 2011 16:32
Paperclip migration rename, add and remove helpers
class UserRenameAvatarToProfileRemoveAAddB < ActiveRecord::Migration
# you may want to change these
AttachmentColumns = [["file_name", :string], ["content_type", :string], ["file_size", :integer]]
# make sure this matches your setup, also have a look in the rename and remove methods
AttachmentPath = Rails.root.join("public", "system")
def self.rename_attachment(table, old, new)
AttachmentColumns.each do |suffix, type|
rename_column table, "#{old}_#{suffix}", "#{new}_#{suffix}"
end
@wader
wader / mysqlselectdump.sh
Created September 24, 2011 12:49
Output select query as insert statements
#!/bin/bash
# Output select query as insert statements
#
# Ex: insert all users from database1 into database2 but replace password with "dummy"
# ./mysqlselectdump username database1 "SELECT id, name, \"dummy\" as password FROM users" users password | mysql -u username -ppassword database2
USERNAME="$1"
DATABASE="$2"
QUERY="$3"
INSERTTABLE="$4"
@wader
wader / gist:1245074
Created September 27, 2011 13:45
git push post-receive capistrano deploy hook script
#!/bin/bash
# requires that you use "set :deploy_via, :remote_cache" in deploy.rb
while read oldrev newrev ref
do
if [ "$ref" = "refs/heads/master" ] ; then
echo "Master branch pushed, deploying to staging"
# seams to be set to "." for hooks, unset to make things more normal
unset GIT_DIR
# deploy path, where "current", "releases", "shared" etc are
@wader
wader / gist:1299311
Created October 19, 2011 18:59
Paperclip change path migration helper
# WARNING: don't just use this without testing!
# This is not a database migration but i think it's quite convenient to do as a migration
# multiple styles has not been tested but should work
class ChangeAttachmentPaths < ActiveRecord::Migration
AttachmentPath = Rails.root.join("public", "system")
def self.paperclip_change_path(table, column, old_opt, new_opt)
model = table.to_s.classify.constantize
@wader
wader / gist:1555889
Created January 3, 2012 17:19
Wav merge
// a bit hackish way of merging two wav files, assumes raw samples.
#import <Foundation/Foundation.h>
#define RIFF_ID 0x52494646 // "RIFF"
#define RIFF_FMT_ID 0x666d7420 // "fmt "
#define RIFF_DATA_ID 0x64617461 // "data"
typedef struct riffChunkHeader {
UInt32 rsc_id; // big endian
@wader
wader / printmethods.awk
Created February 8, 2012 17:10
Print class and instance methods declarations from Objective-C implementation
#!/usr/bin/env awk -f
# print class and instance methods declarations from implementation
# Usage: ./printmethods.awk class.m or awk -f printmethods.awk class.m
/^[[:space:]]*@implementation/ {
implementation = 1;
}
/^[[:space:]]*@end/ {
implementation = 0;
@wader
wader / findselfblocks
Created June 12, 2012 10:05
Help find self references in blocks that might be retain cycles
#!/bin/bash
function check_file() {
echo $1
awk '
/.*/ {
if (inblock > 0) {
collect = collect $0 "\n";
}
line = line + 1
@wader
wader / gist:3059225
Created July 6, 2012 09:37
Binary search array representing "continuous" ranges. {0, 10, 20} = 0-9, 10-19, 20-infinite
static NSUInteger binary_search_range_index(NSUInteger *positions,
NSUInteger len,
NSUInteger n) {
NSInteger current = len / 2;
NSUInteger delta = len / 2;
while (!(n >= positions[current] &&
(current == len-1 || n < positions[current+1]))) {
delta = delta / 2;
if (delta == 0) {
@wader
wader / gist:3622580
Created September 4, 2012 15:42
NSObject category with method to get class of a property
@interface NSObject (classNameOfPropetyName)
- (Class)classOfPropetyName:(NSString *)propertyName;
@end
@implementation NSObject (classNameOfPropetyName)
- (Class)classOfPropetyName:(NSString *)propertyName {
static NSRegularExpression *re = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// T@"NSString",&,N,Vtest
re = [[NSRegularExpression alloc] initWithPattern:@"@\"([^\"]*)\""
@wader
wader / gist:4056077
Created November 11, 2012 20:05
-[NSString stringByResolvingCanonicalPath]
@interface NSString (stringByResolvingCanonicalPath)
- (NSString *)stringByResolvingCanonicalPath;
@end
@implementation NSString (stringByResolvingCanonicalPath)
- (NSString *)stringByResolvingCanonicalPath {
FSRef fsRef;
if (FSPathMakeRef((UInt8 const *)[self UTF8String], &fsRef, NULL) != 0) {
return nil;