Skip to content

Instantly share code, notes, and snippets.

@dfox
dfox / AsynchronousTask.java
Created April 27, 2011 15:30
A contrived asynchronous example class
public interface Callback {
void completed();
}
public class AsynchronousTask extends Thread {
private Callback callback;
private int result;
public AsynchronousTask(final Callback callback){
@dfox
dfox / AsynchronousTaskTest.java
Created April 27, 2011 15:37
A unit test for a contrived asynchronous example class
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
public class AsynchronousTaskTest implements Callback {
private CountDownLatch latch;
public void completed() {
@dfox
dfox / route53-user-policy.json
Created January 25, 2012 16:45
An IAM policy for Amazon AWS to allow limited access to Route 53
{
"Statement":[
{
"Action":[
"route53:ChangeResourceRecordSets",
"route53:GetHostedZone",
"route53:ListResourceRecordSets"
],
"Effect":"Allow",
"Resource":[
@dfox
dfox / update-route53-dns-config.sh
Created January 25, 2012 17:12
Configuration file for the Route 53 update script
AWS_ACCESS_KEY_ID="<Your access key ID>"
AWS_SECRET_ACCESS_KEY="<Your secret access key>"
ZONE="<The name of your subdomain>"
TTL="600"
@dfox
dfox / update-route53-dns.sh
Created January 25, 2012 17:19
A script to update DNS on Route 53
#!/bin/sh
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Load configuration
. /etc/route53/config
@dfox
dfox / adding-properties-to-uiresponder.m
Created August 7, 2012 18:37
Adding Properties to UIResponder
#import "UIResponder+EventInterceptor.h"
#import <objc/runtime.h>
const char *ASSOC_OBJECT_EVENT_TYPE = "eventType";
const char *ASSOC_OBJECT_EVENT_VALUE = "eventValue";
@implementation UIResponder (EventInterceptor)
-(void) setEventValue:(NSString *)eventValue
{
@dfox
dfox / uiapplication-method-swizzling.m
Created August 7, 2012 18:42
Method Swizzling in UIApplication to Capture Touch Events
#import "UIApplication+EventInterceptor.h"
#import <objc/runtime.h>
#import "EventLogger.h"
@implementation UIApplication (EventInterceptor)
+(void) load
{
//Swap the implementations of our interceptor and the original sendEvent:
Method oldMethod = class_getInstanceMethod(self, @selector(sendEvent:));
@dfox
dfox / uiviewcontroller-method-swizzling.m
Created August 7, 2012 18:47
Method Swizzling in UIApplication to Capture viewDidAppear:
#import "UIViewController+EventInterceptor.h"
#import "EventLogger.h"
#import <objc/runtime.h>
@implementation UIViewController (EventInterceptor)
+(void) load
{
//Replace the noop viewDidAppear with our own implementation
class_replaceMethod(self, @selector(viewDidAppear:), (IMP) viewDidAppear, "v@:@");
@dfox
dfox / event-logger.m
Created August 7, 2012 18:56
The EventLogger Class
#import "EventLogger.h"
NSString *const EVENT_LOGGER_APPEARED = @"appeared";
NSString *const EVENT_LOGGER_TOUCHED = @"touched";
@implementation EventLogger
+(void)logEvent:(NSString *)type forObject:(NSObject *)object
{
if ([object respondsToSelector:@selector(eventValue)]
@dfox
dfox / cakeConfig.scala
Last active December 24, 2015 10:09
Externalizing configuration values using the Cake Pattern
/* Standard component trait a-la cake */
trait MessagePrinterComponent {
def printer: MessagePrinter
trait MessagePrinter{
def printMessage(message: String)
}
}