Skip to content

Commit

Permalink
Added debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
georges committed Mar 10, 2012
1 parent e8401f1 commit 0b21d49
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Framework/MCResource/MCAssociation.j
Expand Up @@ -28,4 +28,4 @@
return "<" + class_getName(isa) + " 0x" + [CPString stringWithHash:[self UID]] + ": " + _associationName + " on " + [_parent className] + ">";
}

@end
@end
12 changes: 7 additions & 5 deletions Framework/MCResource/MCBelongsToAssociation.j
Expand Up @@ -9,22 +9,24 @@
{
_associatedObject = nil;
}

return self;
}

- (MCResource)associatedObject
{
CPLog.trace("MCBelongsToAssociation.associatedObject");

if(_associatedObject)
return _associatedObject;

var parentalAssociationId = [_parent valueForKey:_associationName + "Id"];

if(!parentalAssociationId)
return nil;

_associatedObject = [MCResource getResourceWithId:parentalAssociationId ofClass:_associatedObjectClass];

return _associatedObject;
}

Expand All @@ -33,4 +35,4 @@
_associatedObject = associatedObject;
}

@end
@end
58 changes: 30 additions & 28 deletions Framework/MCResource/MCHTTPRequest.j
Expand Up @@ -28,23 +28,23 @@
* },
* "id": 3
* } };
*
*
* // Construct a request
* var saveRequest = [MCHTTPRequest requestTarget:[CPURL URLWithString:@"/orders/123"]
* withMethod:@"PUT"
* andDelegate:self];
*
*
* // Convert our test values to a CPDictionary
* var testCPDict = [CPDictionary dictionaryWithJSObject:testDict recursively:YES];
*
* // Set data to be transmitted
*
* // Set data to be transmitted
* [saveRequest setData:testCPDict];
*
*
* // And finally, start the request
* [saveRequest start];
*
*/

// File-scoped variables -- class variables
var MCHTTPRequestDelegate = nil;
var MCHTTPRequestDelegateRespondsToAuthorizationCredentials = NO;
Expand Down Expand Up @@ -77,7 +77,7 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
+ (void)setDelegate:(id)aDelegate
{
MCHTTPRequestDelegate = aDelegate;

if([aDelegate respondsToSelector:@selector(authorizationCredentials)])
{
MCHTTPRequestDelegateRespondsToAuthorizationCredentials = YES;
Expand All @@ -104,7 +104,7 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
[aRequest setDelegate:aDelegate];
[aRequest setURL:aTarget];
[aRequest setHTTPMethod:aMethod];

return aRequest;
}

Expand All @@ -116,7 +116,7 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
// Set default values
dataTransformer = MCHTTPRequestCachedDefaultTransformer;
HTTPMethod = @"GET";

// Set default headers
_HTTPHeaderFields = [CPDictionary dictionary];

Expand All @@ -130,19 +130,19 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
[_HTTPHeaderFields setValue:"no-cache" forKey:"Cache-Control"];
[_HTTPHeaderFields setValue:"XMLHttpRequest" forKey:"X-Requested-With"];

// Add authorization credentials if
// a) they were specified through setAuthorizationCredentials:
// Add authorization credentials if
// a) they were specified through setAuthorizationCredentials:
// b) none were specified but the class delegate can supply them.
if(authorizationCredentials)
{
[_HTTPHeaderFields setValue:authorizationCredentials forKey:@"Authorization"];
}
else if(MCHTTPRequestDelegateRespondsToAuthorizationCredentials)
{
[_HTTPHeaderFields setValue:[[[self class] delegate] authorizationCredentials] forKey:@"Authorization"];
[_HTTPHeaderFields setValue:[[[self class] delegate] authorizationCredentials] forKey:@"Authorization"];
}
}

return self;
}

Expand All @@ -164,7 +164,7 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
{
// Conserve memory
delete _formData;

// Build new formData
_formData = [dataTransformer transformedData:someData];
}
Expand All @@ -174,7 +174,7 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
{
if(!_formData)
_formData = new FormData();

_formData.append(aKey, value);
}

Expand All @@ -198,13 +198,13 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
{
if(HTTPMethod === aMethod)
return;

// Make sure the given method is valid
if(!aMethod || !aMethod.match(/(put|get|delete|post)/i))
{
throw [MCError errorWithDescription:@"Unknown HTTP request method: '" + aMethod + "'"];
throw [MCError errorWithDescription:@"Unknown HTTP request method: '" + aMethod + "'"];
}

HTTPMethod = aMethod;
}

Expand All @@ -231,15 +231,17 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
//
- (BOOL)start
{
CPLog.trace("MCHTTPRequest.start");

if(_connection)
{
CPLog.warn("Connection has already started");
return YES;
}

error = nil;
_connection = [[MCURLConnection alloc] initWithRequest:self delegate:self startImmediately:YES];

if(_connection)
return YES;
else
Expand Down Expand Up @@ -267,15 +269,15 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
#pragma mark NSURLConnection delegate methods

/*
* CPURLConnection delegate methods (used by MCURLConnection as well)
* CPURLConnection delegate methods (used by MCURLConnection as well)
* For documentation of these methods, see Cappuccino docs
*/
*/
- (void)connection:(CPURLConnection)aConnection didFailWithError:(id)anError
{
if(delegate)
{
error = anError;
[delegate requestDidFail:self];
[delegate requestDidFail:self];
}
else
{
Expand All @@ -286,7 +288,7 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
- (void)connection:(CPURLConnection)aConnection didReceiveResponse:(CPHTTPURLResponse)aResponse
{
_response = aResponse;

if(![aResponse isKindOfClass:[CPHTTPURLResponse class]])
{
CPLog.warn("Expected a CPHTTPURLResponse, but got '" + [aResponse class] + "' instead. Status codes are probably not reliable.");
Expand All @@ -312,10 +314,10 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
else
responseData = _responseData;
}

[[CPNotificationCenter defaultCenter] postNotificationName:MCHTTPRequestDidFinishNotificationName
object:self];

if(delegate)
{
[delegate requestDidFinish:self];
Expand All @@ -340,7 +342,7 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
- (void)connection:(CPURLConnection)connection progressDidChange:(float)progress
{
[[CPNotificationCenter defaultCenter] postNotificationName:MCHTTPRequestDidChangeProgressNotificationName
object:self
object:self
userInfo:[CPDictionary dictionaryWithObject:progress forKey:@"progress"]];
}

Expand All @@ -350,4 +352,4 @@ var MCHTTPRequestCachedDefaultTransformer = [MCJSONDataTransformer new];
throw [MCError errorWithDescription:@"Access denied to " + URL + " by server with 401 Forbidden"];
}

@end
@end
19 changes: 10 additions & 9 deletions Framework/MCResource/MCQueuedRequest.j
Expand Up @@ -6,15 +6,15 @@
CPSet childRequests @accessors;
BOOL blocking @accessors;
float progress @accessors(readonly);

CPSet finishedChildRequests;
id originalDelegate;
}

// Constructor method for clean code (TM)
+ (MCQueuedRequest)queuedRequestWithRequest:(MCHTTPRequest)aRequest
{
var queuedRequest = [[MCQueuedRequest alloc] initWithRequest:aRequest];
var queuedRequest = [[MCQueuedRequest alloc] initWithRequest:aRequest];
return queuedRequest;
}

Expand All @@ -27,7 +27,7 @@
finishedChildRequests = [CPSet set];
[self setHTTPRequest:aRequest];
}

return self;
}

Expand All @@ -52,8 +52,8 @@
// completion of the original request
originalDelegate = [aRequest delegate];
[aRequest setDelegate:self];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(requestDidChangeProgress:) name:MCHTTPRequestDidChangeProgressNotificationName object:aRequest];

[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(requestDidChangeProgress:) name:MCHTTPRequestDidChangeProgressNotificationName object:aRequest];
}

// Add a child request to be executed upon completion of this request.
Expand Down Expand Up @@ -94,7 +94,7 @@
{
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(childRequestDidFinish:) name:MCHTTPRequestDidFinishNotificationName object:[childRequest HTTPRequest]];
[[self queue] appendRequest:childRequest];
}
}
}
}

Expand All @@ -119,6 +119,7 @@

- (void)requestDidFail:(MCHTTPRequest)aRequest
{
CPLog.trace("MCQueuedRequest.requestDidFail");
[originalDelegate requestDidFail:self];
}

Expand All @@ -129,13 +130,13 @@
{
var description = @"<MCQueuedRequest 0x" + [CPString stringWithHash:[self UID]] + ": " + [HTTPRequest HTTPMethod] + " to " + [HTTPRequest URL] + ">",
childRequestArray = [childRequests allObjects];

for(var i = 0; i < [childRequestArray count]; i++)
{
description += @"\n\tChild request: " + [[childRequestArray objectAtIndex:i] description];
}

return description;
}

@end
@end
17 changes: 13 additions & 4 deletions Framework/MCResource/MCResource.j
Expand Up @@ -318,23 +318,25 @@ var AllResourcesByTypeAndId = [CPDictionary dictionary];
return self;
}


// This is a standard override for development purposes - if log output gets too messy, remove it.
- (CPString)description
{
var description = [self className] + ", ID " + [self identifier] + ": { \n",
var description = [self className] + ", ID " + [self identifier] + ": { ",
attributes = [[[self class] attributes] keyEnumerator],
attribute;

while(attribute = [attributes nextObject])
{
description += "\t " + attribute + " = '" + [self valueForKey:attribute] + "'\n";
description += attribute + " " //+ " = '" + [self valueForKey:attribute] + "'\n";
}

description += "};";

return description;
}


#pragma mark -
#pragma mark Public methods

Expand All @@ -345,6 +347,8 @@ var AllResourcesByTypeAndId = [CPDictionary dictionary];

+ (MCResource)getResourceWithId:(int)anIdentifier ofClass:(Class)resourceClass
{
CPLog.trace("MCResource.getResourceWithId");

return [AllResourcesByTypeAndId valueForKeyPath:resourceClass + "." + anIdentifier];
}

Expand Down Expand Up @@ -629,7 +633,7 @@ var AllResourcesByTypeAndId = [CPDictionary dictionary];
}
else
{
// CPLog.warn(@"Resource " + self + " loaded without identifier key! This will likely cause problems in the future.");
CPLog.warn(@"Resource " + self + " loaded without identifier key! This will likely cause problems in the future.");
}

// And normal treatment for the other resources
Expand Down Expand Up @@ -1300,6 +1304,8 @@ var AllResourcesByTypeAndId = [CPDictionary dictionary];

+ (void)requestDidFail:(MCQueuedRequest)aRequest
{
CPLog.trace("MCResource+requestDidFail");

var aDelegate = [classMethodDelegateDictionary objectForKey:aRequest],
aSelector = [classMethodSelectorDictionary objectForKey:aRequest];

Expand All @@ -1322,6 +1328,8 @@ var AllResourcesByTypeAndId = [CPDictionary dictionary];
// Let's hope this doesn't get called to often ;)
- (void)requestDidFail:(MCQueuedRequest)aRequest
{
CPLog.trace("MCResource.requestDidFail");

var aDelegate = [_instanceMethodDelegateDictionary objectForKey:aRequest],
aSelector = [_instanceMethodSelectorDictionary objectForKey:aRequest];

Expand Down Expand Up @@ -1355,7 +1363,8 @@ var AllResourcesByTypeAndId = [CPDictionary dictionary];
informativeTextWithFormat:MCResourceGeneralErrorDetailedMessage];

[alert setDelegate:self];
[alert beginSheetModalForWindow:[CPApp mainWindow]];
if (CPApp)
[alert beginSheetModalForWindow:[CPApp mainWindow]];

_MCResourceErrorAlertIsShowing = YES;
}
Expand Down

0 comments on commit 0b21d49

Please sign in to comment.