Skip to content

Instantly share code, notes, and snippets.

# Example class by christophercotton to show how to validate JSON in
# ActiveRecord
class Stuff < ActiveRecord::Base
validates_presence_of :my_field
validate :valid_json
# make sure the JSON is valid
def valid_json
begin
@christophercotton
christophercotton / UIImage+Resize.m
Created July 16, 2010 17:00
Using http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ but with additions for iPhone 4 hires scale images. Also includes fix for 24bit non alpha images.
// Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
// The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
// If the new size is not integral, it will be rounded up
- (UIImage *)resizedImage:(CGSize)newSize
transform:(CGAffineTransform)transform
drawTransposed:(BOOL)transpose
interpolationQuality:(CGInterpolationQuality)quality {
// COTTON - adding in scaling for iPhone 4
BOOL shouldScale = NO;
@christophercotton
christophercotton / PausableAdhwirlLayout.java
Created March 7, 2011 17:59
AdwhirlLayout that has a hack to pause the ad rotation
import android.app.Activity;
import com.adwhirl.AdWhirlLayout;
public class PausableAdhwirlLayout extends AdWhirlLayout {
public PausableAdhwirlLayout(final Activity context, final String keyAdWhirl) {
super(context, keyAdWhirl);
}
/**
@christophercotton
christophercotton / OpaqueLayer.h
Created January 5, 2012 04:25
Basic Cocos2d Layer to swallow all the touch events on it
// Feel free to use. MIT License
#import <Foundation/Foundation.h>
#import "cocos2d.h"
// Layer that will just capture any touch events on it
@interface OpaqueLayer : CCLayerColor {
}
@christophercotton
christophercotton / About.md
Created April 2, 2013 14:51
Apple GameKit Error 5003 during

I was getting the error:

Error Domain=GKErrorDomain Code=3 "The requested operation could not be completed due to an error communicating with the server." UserInfo=0xb1935a0 {GKServerStatusCode=5003, NSUnderlyingError=0xb1866b0 "The operation couldn’t be completed. status = 5003, Found turn sequence at index: 1 that will never be reached because infinite timeout has specified for previous sequence", NSLocalizedDescription=The requested operation could not be completed due to an error communicating with the server.}

When trying to end the match with

  [match endTurnWithNextParticipants:nextParticipants turnTimeout:GKTurnTimeoutNone matchData:data completionHandler: ^(NSError *error) {
@christophercotton
christophercotton / Article.rb
Created June 2, 2013 17:55
Article sample for PG Search
class Article < ActiveRecord::Base
attr_accessible :body, :title
end
class Article < ActiveRecord::Base
attr_accessible :body, :title
include PgSearch
end
@christophercotton
christophercotton / App icons.jstalk
Last active December 30, 2015 00:59 — forked from douglashill/App icons.jstalk
Modified the gist to allow it to work with the front most document.
function main(image, doc, layer) {
var sizes = [29, 40, 50, 57, 58, 72, 76, 80, 100, 114, 120, 144, 152, 1024];
for (var idx = 0; idx < sizes.length; idx++) {
doc.scaleImageToWidth(sizes[idx]);
doc.dataRepresentationOfType("public.png").writeToFile("/Users/cotton/Desktop/icon-" + sizes[idx] + ".png");
doc.undo();
}
}
@christophercotton
christophercotton / ViewController.swift
Created January 17, 2015 02:49
Display BLE Advertisement Data and when it changes
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager:CBCentralManager!
var queue:dispatch_queue_t!
var datas = [String : String]()
var peripherals = [CBPeripheral]()
override func viewDidLoad() {
@christophercotton
christophercotton / UIView+Inspectable.swift
Created August 28, 2015 14:54
Adds inspectable properties to allow editing of the border and corner radius
// UIView+Inspectable.swift
//
// Created by Christopher Cotton. No Copyright
import UIKit
extension UIView {
@IBInspectable var borderColor:UIColor {
set {
@christophercotton
christophercotton / DebugViews.swift
Created August 28, 2015 14:58
Debug versions of views which can be used to track down positioning issues
// DebugViews.swift
// Created by Christopher Cotton, no copyright
import UIKit
class DebugView: UIView {
override var bounds: CGRect {
didSet {
println("Bounds Changed: \(bounds)")