Skip to content

Instantly share code, notes, and snippets.

@natemartinsf
Created April 9, 2010 18:34
Show Gist options
  • Save natemartinsf/361442 to your computer and use it in GitHub Desktop.
Save natemartinsf/361442 to your computer and use it in GitHub Desktop.
@implementation NMValidatedObjectProxy : CPProxy
{
NMValidatedObject _realObject;
BOOL isFault;
}
-(id)initWithValidatedObject:(NMValidatedObject)theObject
{
_realObject = theObject;
CPLog(@"Initing proxy. Real object is: %@", _realObject);
isFault = ![_realObject _loaded];
return self;
}
+ (BOOL)respondsToSelector:(SEL)aSelector
{
return [[_realObject class] respondsToSelector:aSelector];
}
- (CPMethodSignature)methodSignatureForSelector:(SEL)selector
{
// does the delegate respond to this selector?
var selectorString = CPStringFromSelector(selector);
if ([_realObject respondsToSelector:selector])
{
// yes, return the delegate's method signature
return YES;
} else {
CPLog(@"Actually a missing method");
// no, return whatever CPObject would return
return [super methodSignatureForSelector: selector];
}
}
- (id)forwardInvocation:(CPInvocation)anInvocation
{
var aSelector = [anInvocation selector];
if ([_realObject respondsToSelector:aSelector])
{
[anInvocation invokeWithTarget:_realObject];
CPLog(@"Return value is: %@", [anInvocation returnValue]);
return [anInvocation returnValue];
}
else
{
[self doesNotRecognizeSelector:aSelector];
return null;
}
}
@end
@implementation NMValidatedObject : CPObject
{
//...
}
-(id)init
{
self = [super init];
var proxy;
if(self)
{
//...
CPLog(@"Creating proxy");
proxy = [[NMValidatedObjectProxy alloc] initWithValidatedObject:self];
}
if(proxy)
{
return proxy;
}
else
{
return self;
}
}
//...
@end
@implementation NMJobPosting : NMValidatedObject
{
//...
}
- (id)init
{
if(self = [super init])
{
//...
}
return self;
}
- (void)testMethod
{
CPLog(@"test method got called!");
}
- (CPString)testReturn
{
CPLog(@"test return called");
return @"the return value";
}
@end
@implementation NMJobListController : CPObject
{
//...
}
- (void)awakeFromCib
{
CPLog(@"Creating test post");
var testPosting = [[NMJobPosting alloc] init];
CPLog(@"trying test method");
[testPosting testMethod];
CPLog(@"title is: %@", [testPosting title]);
CPLog(@"testing return method - return value: %@", [testPosting testReturn]);
}
@end
/* outputs:
010-03-09 11:30:10.377 Cappuccino: Creating test post
2010-03-09 11:30:10.378 Cappuccino: Creating proxy
2010-03-09 11:30:10.379 Cappuccino: Initing proxy. Real object is: null
2010-03-09 11:30:10.388 Cappuccino: trying test method
2010-03-09 11:30:10.389 Cappuccino: test method got called!
2010-03-09 11:30:10.390 Cappuccino: test return called
2010-03-09 11:30:10.391 Cappuccino: Return value is: the return value
2010-03-09 11:30:10.392 Cappuccino: testing return method - return value: undefined
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment