From 42ae4f8129444c56f761fb5f3931324341de9c5b Mon Sep 17 00:00:00 2001 From: vickeryj Date: Thu, 29 Jan 2009 14:31:12 -0500 Subject: [PATCH] [#9] move everything to a category on NSObject --- Classes/Dog.m | 8 +- Classes/DogErrorTest.m | 4 +- Classes/DogTest.m | 4 +- Classes/EditPersonController.m | 4 + Classes/Person.m | 4 +- Classes/lib/NSObject+ObjectiveResource.h | 74 +++++ Classes/lib/NSObject+ObjectiveResource.m | 300 ++++++++++++++++++ ...bjectiveResource+JSONSerializableSupport.h | 13 - ...bjectiveResource+JSONSerializableSupport.m | 17 - ...ObjectiveResource+XMLSerializableSupport.h | 15 - ...ObjectiveResource+XMLSerializableSupport.m | 21 -- Classes/lib/ObjectiveResource.h | 18 +- Classes/lib/ObjectiveResource.m | 297 ++++------------- Classes/lib/objective_support | 2 +- objective_resource.xcodeproj/project.pbxproj | 2 + 15 files changed, 457 insertions(+), 326 deletions(-) create mode 100644 Classes/lib/NSObject+ObjectiveResource.h create mode 100644 Classes/lib/NSObject+ObjectiveResource.m delete mode 100644 Classes/lib/ObjectiveResource+JSONSerializableSupport.h delete mode 100644 Classes/lib/ObjectiveResource+JSONSerializableSupport.m delete mode 100644 Classes/lib/ObjectiveResource+XMLSerializableSupport.h delete mode 100644 Classes/lib/ObjectiveResource+XMLSerializableSupport.m diff --git a/Classes/Dog.m b/Classes/Dog.m index a098d52..d914e5f 100644 --- a/Classes/Dog.m +++ b/Classes/Dog.m @@ -33,19 +33,19 @@ - (void) dealloc #pragma mark ObjectiveResource overrides to handle nestd resources -+ (NSString *)collectionName { ++ (NSString *)getORSCollectionName { return @"people"; } -- (BOOL)createWithResponse:(NSError **)aError { +- (BOOL)createORSWithResponse:(NSError **)aError { return [self createAtPath:[[self class] elementPath:[self nestedPath]] withResponse:aError]; } -- (BOOL)updateWithResponse:(NSError **)aError { +- (BOOL)updateORSWithResponse:(NSError **)aError { return [self updateAtPath:[[self class] elementPath:[self nestedPath]] withResponse:aError]; } -- (BOOL)destroyWithResponse:(NSError **)aError { +- (BOOL)destroyORSWithResponse:(NSError **)aError { return [self destroyAtPath:[[self class] elementPath:[self nestedPath]] withResponse:aError]; } diff --git a/Classes/DogErrorTest.m b/Classes/DogErrorTest.m index ca2f4e1..39ac8af 100644 --- a/Classes/DogErrorTest.m +++ b/Classes/DogErrorTest.m @@ -10,7 +10,7 @@ @implementation DogError -+ (NSString *)getSite { ++ (NSString *)getORSSite { return @"http://localhost:36313/"; } @@ -18,7 +18,7 @@ + (NSString *)getSite { @implementation DogDoesNotExist -+ (NSString *)getSite { ++ (NSString *)getORSSite { return @"http://badhost.localhost:9999/"; } diff --git a/Classes/DogTest.m b/Classes/DogTest.m index 3cdaa04..ddf0ceb 100644 --- a/Classes/DogTest.m +++ b/Classes/DogTest.m @@ -18,8 +18,8 @@ @implementation DogTest -(void) setUp { [ObjectiveResource setSite:@"http://localhost:36313/"]; - //[ObjectiveResource setResponseType:JSONResponse]; - [ObjectiveResource setResponseType:XmlResponse]; + [ObjectiveResource setResponseType:JSONResponse]; + //[ObjectiveResource setResponseType:XmlResponse]; owner = [Person find:[NSString stringWithFormat:@"%i",DOG_OWNER]]; } diff --git a/Classes/EditPersonController.m b/Classes/EditPersonController.m index cb1ec9c..ffa469c 100644 --- a/Classes/EditPersonController.m +++ b/Classes/EditPersonController.m @@ -11,6 +11,7 @@ @implementation EditPersonController +@synthesize person, aTextField, aViewController; /* // Implement loadView to create a view hierarchy programmatically. @@ -39,6 +40,9 @@ - (void)didReceiveMemoryWarning { - (void)dealloc { + [person release]; + [aTextField release]; + [aViewController release]; [super dealloc]; } diff --git a/Classes/Person.m b/Classes/Person.m index 5dc3f99..6904145 100644 --- a/Classes/Person.m +++ b/Classes/Person.m @@ -15,7 +15,7 @@ @implementation Person // handle pluralization -+ (NSString *)collectionName { ++ (NSString *)getORSCollectionName { return @"people"; } @@ -23,7 +23,7 @@ + (NSString *)collectionName { // this will go to the url http://localhost:3000/people//dogs // and return the array of dogs -(NSArray *) findAllDogs { - return [Person find:[NSString stringWithFormat:@"%@/%@",personId,@"dogs",nil]]; + return [Dog find:[NSString stringWithFormat:@"%@/%@",personId,@"dogs",nil]]; } @end diff --git a/Classes/lib/NSObject+ObjectiveResource.h b/Classes/lib/NSObject+ObjectiveResource.h new file mode 100644 index 0000000..5584b44 --- /dev/null +++ b/Classes/lib/NSObject+ObjectiveResource.h @@ -0,0 +1,74 @@ +// +// NSObject+ObjectiveResource.h +// objectivesync +// +// Created by vickeryj on 1/29/09. +// Copyright 2009 Joshua Vickery. All rights reserved. +// + +#import + +@interface NSObject (ObjectiveResource) + + +// Response Formats +typedef enum { + XmlResponse = 0, + JSONResponse, +} ORSResponseFormat; + +// Resource configuration ++ (NSString *)getORSSite; ++ (void)setORSSite:(NSString*)siteURL; ++ (NSString *)getORSUser; ++ (void)setORSUser:(NSString *)user; ++ (NSString *)getORSPassword; ++ (void)setORSPassword:(NSString *)password; ++ (SEL)getORSParseDataMethod; ++ (void)setORSParseDataMethod:(SEL)parseMethod; ++ (SEL) getORSSerializeMethod; ++ (void) setORSSerializeMethod:(SEL)serializeMethod; ++ (NSString *)getORSProtocolExtension; ++ (void)setORSProtocolExtension:(NSString *)protocolExtension; ++ (void)setORSResponseType:(ORSResponseFormat) format; ++ (ORSResponseFormat)getORSResponseType; + + +// Finders ++ (NSArray *)findAllORS; ++ (NSArray *)findAllORSWithResponse:(NSError **)aError; ++ (id)findORS:(NSString *)elementId; ++ (id)findORS:(NSString *)elementId withResponse:(NSError **)aError; + +// URL construction accessors ++ (NSString *)getORSElementName; ++ (NSString *)getORSCollectionName; ++ (NSString *)getORSElementPath:(NSString *)elementId; ++ (NSString *)getORSCollectionPath; ++ (NSString *)getORSCollectionPathWithParameters:(NSDictionary *)parameters; ++ (NSString *)populateORSPath:(NSString *)path withParameters:(NSDictionary *)parameters; + +// Instance-specific methods +- (id)getORSId; +- (NSString *)getORSClassIdName; +- (BOOL)createORS; +- (BOOL)createORSWithResponse:(NSError **)aError; +- (BOOL)createORSWithParameters:(NSDictionary *)parameters; +- (BOOL)createORSWithParameters:(NSDictionary *)parameters andResponse:(NSError **)aError; +- (BOOL)destroyORS; +- (BOOL)destroyORSWithResponse:(NSError **)aError; +- (BOOL)updateORS; +- (BOOL)updateORSWithResponse:(NSError **)aError; +- (BOOL)saveORS; +- (BOOL)saveORSWithResponse:(NSError **)aError; + + +- (BOOL)createORSAtPath:(NSString *)path withResponse:(NSError **)aError; +- (BOOL)updateORSAtPath:(NSString *)path withResponse:(NSError **)aError; +- (BOOL)destroyORSAtPath:(NSString *)path withResponse:(NSError **)aError; + +// Instance helpers for getting at commonly used class-level values +- (NSString *)getORSCollectionPath; +- (NSString *)convertToORSExpectedType; + +@end diff --git a/Classes/lib/NSObject+ObjectiveResource.m b/Classes/lib/NSObject+ObjectiveResource.m new file mode 100644 index 0000000..4ac40ee --- /dev/null +++ b/Classes/lib/NSObject+ObjectiveResource.m @@ -0,0 +1,300 @@ +// +// NSObject+ObjectiveResource.m +// objectivesync +// +// Created by vickeryj on 1/29/09. +// Copyright 2009 Joshua Vickery. All rights reserved. +// + +#import "NSObject+ObjectiveResource.h" +#import "Connection.h" +#import "Response.h" +#import "CoreSupport.h" +#import "XMLSerializableSupport.h" +#import "JSONSerializableSupport.h" + +static NSString *_activeResourceSite = nil; +static NSString *_activeResourceUser = nil; +static NSString *_activeResourcePassword = nil; +static SEL _activeResourceParseDataMethod = nil; +static SEL _activeResourceSerializeMethod = nil; +static NSString *_activeResourceProtocolExtension = @".xml"; +static ORSResponseFormat _format; + +@implementation NSObject (ObjectiveResource) + +#pragma mark configuration methods ++ (NSString *)getORSSite { + return _activeResourceSite; +} + ++ (void)setORSSite:(NSString *)siteURL { + _activeResourceSite = siteURL; +} + ++ (NSString *)getORSUser { + return _activeResourceUser; +} + ++ (void)setORSUser:(NSString *)user { + _activeResourceUser = user; +} + ++ (NSString *)getORSPassword { + return _activeResourcePassword; +} + ++ (void)setORSPassword:(NSString *)password { + _activeResourcePassword = password; +} + ++ (void)setORSResponseType:(ORSResponseFormat) format { + _format = format; + switch (format) { + case JSONResponse: + [[self class] setORSProtocolExtension:@".json"]; + [[self class] setORSParseDataMethod:@selector(fromJSONData:)]; + [[self class] setORSSerializeMethod:@selector(toJSONExcluding:)]; + break; + default: + [[self class] setORSProtocolExtension:@".xml"]; + [[self class] setORSParseDataMethod:@selector(fromXMLData:)]; + [[self class] setORSSerializeMethod:@selector(toXMLElementExcluding:)]; + break; + } +} + ++ (ORSResponseFormat)getORSResponseType { + return _format; +} + ++ (SEL)getORSParseDataMethod { + return (nil == _activeResourceParseDataMethod) ? @selector(fromXMLData:) : _activeResourceParseDataMethod; +} + ++ (void)setORSParseDataMethod:(SEL)parseMethod { + _activeResourceParseDataMethod = parseMethod; +} + ++ (SEL) getORSSerializeMethod { + return (nil == _activeResourceSerializeMethod) ? @selector(toXMLElementExcluding:) : _activeResourceSerializeMethod; +} + ++ (void) setORSSerializeMethod:(SEL)serializeMethod { + _activeResourceSerializeMethod = serializeMethod; +} + ++ (NSString *)getORSProtocolExtension { + return _activeResourceProtocolExtension; +} + ++ (void)setORSProtocolExtension:(NSString *)protocolExtension { + _activeResourceProtocolExtension = protocolExtension; +} + + +// Find all items ++ (NSArray *)findAllORSWithResponse:(NSError **)aError { + Response *res = [Connection get:[self getORSCollectionPath] withUser:[[self class] getORSUser] andPassword:[[self class] getORSPassword]]; + if([res isError] && aError) { + *aError = res.error; + } + return [self performSelector:[self getORSParseDataMethod] withObject:res.body]; +} + ++ (NSArray *)findAllORS { + NSError *aError; + return [self findAllORSWithResponse:&aError]; +} + ++ (id)findORS:(NSString *)elementId withResponse:(NSError **)aError { + Response *res = [Connection get:[self getORSElementPath:elementId] withUser:[[self class] getORSUser] andPassword:[[self class] getORSPassword]]; + if([res isError] && aError) { + *aError = res.error; + } + return [self performSelector:[self getORSParseDataMethod] withObject:res.body]; +} + ++ (id)findORS:(NSString *)elementId { + NSError *aError; + return [self findORS:elementId withResponse:&aError]; +} + ++ (NSString *)getORSElementName { + return [[NSStringFromClass([self class]) stringByReplacingCharactersInRange:NSMakeRange(0, 1) + withString:[[NSStringFromClass([self class]) substringWithRange:NSMakeRange(0,1)] lowercaseString]] underscore]; +} + ++ (NSString *)getORSCollectionName { + return [[self getORSElementName] stringByAppendingString:@"s"]; +} + ++ (NSString *)getORSElementPath:(NSString *)elementId { + return [NSString stringWithFormat:@"%@%@/%@%@", [self getORSSite], [self getORSCollectionName], elementId, [self getORSProtocolExtension]]; +} + ++ (NSString *)getORSCollectionPath { + return [[[self getORSSite] stringByAppendingString:[self getORSCollectionName]] stringByAppendingString:[self getORSProtocolExtension]]; +} + ++ (NSString *)getORSCollectionPathWithParameters:(NSDictionary *)parameters { + return [self populateORSPath:[self getORSCollectionPath] withParameters:parameters]; +} + ++ (NSString *)populateORSPath:(NSString *)path withParameters:(NSDictionary *)parameters { + + // Translate each key to have a preceeding ":" for proper URL param notiation + NSMutableDictionary *parameterized = [NSMutableDictionary dictionaryWithCapacity:[parameters count]]; + for (NSString *key in parameters) { + [parameterized setObject:[parameters objectForKey:key] forKey:[NSString stringWithFormat:@":%@", key]]; + } + return [path gsub:parameterized]; +} + +- (NSString *)getORSCollectionPath { + return [[self class] getORSCollectionPath]; +} + +// Converts the object to the data format expected by the server +- (NSString *)convertToORSExpectedType { + // exclude id , created_at , updated_at + NSArray *defaultExclusions = [NSArray arrayWithObjects:[self getORSClassIdName],@"createdAt",@"updatedAt",nil]; + return [self performSelector:[[self class] getORSSerializeMethod] withObject:defaultExclusions]; +} + +#pragma mark default equals methods for id and class based equality +- (BOOL)isEqual:(id)anObject { + return [NSStringFromClass([self class]) isEqualToString:NSStringFromClass([anObject class])] && + [anObject respondsToSelector:@selector(getId)] && [[anObject getORSId] isEqualToString:[self getORSId]]; +} +- (NSUInteger)hash { + return [[self getORSId] intValue] + [NSStringFromClass([self class]) hash]; +} + +#pragma mark Instance-specific methods +- (id)getORSId { + id result = nil; + SEL idMethodSelector = NSSelectorFromString([self getORSClassIdName]); + if ([self respondsToSelector:idMethodSelector]) { + result = [self performSelector:idMethodSelector]; + } + return result; +} + +- (NSString *)getORSClassIdName { + + return [NSString stringWithFormat:@"%@Id", + [NSStringFromClass([self class]) stringByReplacingCharactersInRange:NSMakeRange(0, 1) + withString:[[NSStringFromClass([self class]) substringWithRange:NSMakeRange(0,1)] lowercaseString]]]; + +} + +- (BOOL)createORSAtPath:(NSString *)path withResponse:(NSError **)aError { + Response *res = [Connection post:[self convertToORSExpectedType] to:path withUser:[[self class] getORSUser] andPassword:[[self class] getORSPassword]]; + if([res isError] && aError) { + *aError = res.error; + } + if ([res isSuccess]) { + NSDictionary *newProperties = [[[self class] performSelector:[[self class] getORSParseDataMethod] withObject:res.body] properties]; + [self setProperties:newProperties]; + return YES; + } + else { + return NO; + } +} + +-(BOOL)updateORSAtPath:(NSString *)path withResponse:(NSError **)aError { + Response *res = [Connection put:[self convertToORSExpectedType] to:path + withUser:[[self class] getORSUser] andPassword:[[self class] getORSPassword]]; + if([res isError] && aError) { + *aError = res.error; + } + if ([res isSuccess]) { + if([(NSString *)[res.headers objectForKey:@"Content-Length"] intValue] > 1) { + NSDictionary *newProperties = [[[self class] performSelector:[[self class] getORSParseDataMethod] withObject:res.body] properties]; + [self setProperties:newProperties]; + } + return YES; + } + else { + return NO; + } + +} + +- (BOOL)destroyORSAtPath:(NSString *)path withResponse:(NSError **)aError { + Response *res = [Connection delete:path withUser:[[self class] getORSUser] andPassword:[[self class] getORSPassword]]; + if([res isError] && aError) { + *aError = res.error; + } + return [res isSuccess]; +} + +- (BOOL)createORSWithResponse:(NSError **)aError { + return [self createORSAtPath:[self getORSCollectionPath] withResponse:aError]; +} + +- (BOOL)createORS { + NSError *error; + return [self createORSWithResponse:&error]; +} + +- (BOOL)createORSWithParameters:(NSDictionary *)parameters andResponse:(NSError **)aError { + return [self createORSAtPath:[[self class] getORSCollectionPathWithParameters:parameters] withResponse:aError]; +} + +- (BOOL)createORSWithParameters:(NSDictionary *)parameters { + NSError *error; + return [self createORSWithParameters:parameters andResponse:&error]; +} + + +- (BOOL)destroyORSWithResponse:(NSError **)aError { + id myId = [self getORSId]; + if (nil != myId) { + return [self destroyORSAtPath:[[self class] getORSElementPath:myId] withResponse:aError]; + } + else { + // this should return a error + return NO; + } +} + +- (BOOL)destroyORS { + NSError *error; + return [self destroyORSWithResponse:&error]; +} + +- (BOOL)updateORSWithResponse:(NSError **)aError { + id myId = [self getORSId]; + if (nil != myId) { + return [self updateORSAtPath:[[self class] getORSElementPath:myId] withResponse:aError]; + } + else { + // this should return an error + return NO; + } +} + +- (BOOL)updateORS { + NSError *error; + return [self updateORSWithResponse:&error]; +} + +- (BOOL)saveORSWithResponse:(NSError **)aError { + id myId = [self getORSId]; + if (nil == myId) { + return [self createORSWithResponse:aError]; + } + else { + return [self updateORSWithResponse:aError]; + } +} + +- (BOOL)saveORS { + NSError *error; + return [self saveORSWithResponse:&error]; +} + +@end diff --git a/Classes/lib/ObjectiveResource+JSONSerializableSupport.h b/Classes/lib/ObjectiveResource+JSONSerializableSupport.h deleted file mode 100644 index 2dabbb1..0000000 --- a/Classes/lib/ObjectiveResource+JSONSerializableSupport.h +++ /dev/null @@ -1,13 +0,0 @@ -// -// ObjectiveResource+JSONSerialization.h -// active_resource -// -// Created by James Burka on 1/19/09. -// Copyright 2009 Burkaprojects. All rights reserved. -// -#import "ObjectiveResource.h" - -@interface ObjectiveResource(JSONSerializableSupport) -- (NSString *)toJSONAs:(NSString *)rootName excludingInArray:(NSArray *)exclusions - withTranslations:(NSDictionary *)keyTranslations; -@end diff --git a/Classes/lib/ObjectiveResource+JSONSerializableSupport.m b/Classes/lib/ObjectiveResource+JSONSerializableSupport.m deleted file mode 100644 index 100a0ec..0000000 --- a/Classes/lib/ObjectiveResource+JSONSerializableSupport.m +++ /dev/null @@ -1,17 +0,0 @@ -// -// ObjectiveResource+JSONSerialization.m -// active_resource -// -// Created by James Burka on 1/19/09. -// Copyright 2009 Burkaprojects. All rights reserved. -// - -#import "ObjectiveResource+JSONSerializableSupport.h" -#import "ObjectiveSupport.h" - -@implementation ObjectiveResource(JSONSerializableSupport) -- (NSString *)toJSONAs:(NSString *)rootName excludingInArray:(NSArray *)exclusions - withTranslations:(NSDictionary *)keyTranslations { - return [[self properties] toJSONAs:rootName excludingInArray:exclusions withTranslations:keyTranslations]; -} -@end diff --git a/Classes/lib/ObjectiveResource+XMLSerializableSupport.h b/Classes/lib/ObjectiveResource+XMLSerializableSupport.h deleted file mode 100644 index 40c99ea..0000000 --- a/Classes/lib/ObjectiveResource+XMLSerializableSupport.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// ObjectiveResource+XML.h -// -// -// Created by Ryan Daigle on 7/24/08. -// Copyright 2008 yFactorial, LLC. All rights reserved. -// - -#import "ObjectiveResource.h" - -@interface ObjectiveResource (XMLSerializableSupport) - -- (NSString *)toXMLElementAs:(NSString *)rootName excludingInArray:(NSArray *)exclusions - withTranslations:(NSDictionary *)keyTranslations; -@end diff --git a/Classes/lib/ObjectiveResource+XMLSerializableSupport.m b/Classes/lib/ObjectiveResource+XMLSerializableSupport.m deleted file mode 100644 index a7d86cf..0000000 --- a/Classes/lib/ObjectiveResource+XMLSerializableSupport.m +++ /dev/null @@ -1,21 +0,0 @@ -// -// ObjectiveResource+XML.m -// -// -// Created by Ryan Daigle on 7/24/08. -// Copyright 2008 yFactorial, LLC. All rights reserved. -// - -#import "ObjectiveResource+XMLSerializableSupport.h" -#import "XMLSerializableSupport.h" - -@implementation ObjectiveResource (XMLSerializableSupport) - - -- (NSString *)toXMLElementAs:(NSString *)rootName excludingInArray:(NSArray *)exclusions - withTranslations:(NSDictionary *)keyTranslations { - return [[self properties] toXMLElementAs:rootName excludingInArray:exclusions withTranslations:keyTranslations]; -} - - -@end diff --git a/Classes/lib/ObjectiveResource.h b/Classes/lib/ObjectiveResource.h index 47d5abe..a667079 100644 --- a/Classes/lib/ObjectiveResource.h +++ b/Classes/lib/ObjectiveResource.h @@ -6,15 +6,10 @@ // Copyright 2008 yFactorial, LLC. All rights reserved. // -@interface ObjectiveResource : NSObject - +#import "NSObject+ObjectiveResource.h" -// Response Formats -typedef enum { - XmlResponse = 0, - JSONResponse, -} ResponseFormat; +@interface ObjectiveResource : NSObject // Resource configuration + (NSString *)getSite; + (void)setSite:(NSString*)siteURL; @@ -28,8 +23,8 @@ typedef enum { + (void) setSerializeMethod:(SEL)serializeMethod; + (NSString *)protocolExtension; + (void)setProtocolExtension:(NSString *)protocolExtension; -+ (void)setResponseType:(ResponseFormat) format; -+ (ResponseFormat)getResponseType; ++ (void)setResponseType:(ORSResponseFormat) format; ++ (ORSResponseFormat)getResponseType; // Finders @@ -62,10 +57,7 @@ typedef enum { - (BOOL)createAtPath:(NSString *)path withResponse:(NSError **)aError; -- (BOOL)updateAtPath:(NSString *)path withResponse:(NSError **)aError; +- (BOOL)updateAtPath:(NSString *)path withResponse:(NSError **)aError; - (BOOL)destroyAtPath:(NSString *)path withResponse:(NSError **)aError; -// Instance helpers for getting at commonly used class-level values -- (NSString *)collectionPath; - @end \ No newline at end of file diff --git a/Classes/lib/ObjectiveResource.m b/Classes/lib/ObjectiveResource.m index bb97072..fbe4912 100644 --- a/Classes/lib/ObjectiveResource.m +++ b/Classes/lib/ObjectiveResource.m @@ -7,306 +7,131 @@ // #import "ObjectiveResource.h" -#import "Connection.h" -#import "Response.h" -#import "CoreSupport.h" -#import "XMLSerializableSupport.h" -#import "JSONSerializableSupport.h" - -static NSString *_activeResourceSite = nil; -static NSString *_activeResourceUser = nil; -static NSString *_activeResourcePassword = nil; -static SEL _activeResourceParseDataMethod = nil; -static SEL _activeResourceSerializeMethod = nil; -static NSString *_activeResourceProtocolExtension = @".xml"; -static ResponseFormat _format; - -@interface ObjectiveResource() -- (NSString *)convertToExpectedType; -@end - @implementation ObjectiveResource - -#pragma mark configuration methods +#pragma mark Resource configuration + (NSString *)getSite { - return _activeResourceSite; + return [self getORSSite]; } - -+ (void)setSite:(NSString *)siteURL { - _activeResourceSite = siteURL; ++ (void)setSite:(NSString*)siteURL { + [self setORSSite:siteURL]; } - + (NSString *)getUser { - return _activeResourceUser; + return [self getORSUser]; } - + (void)setUser:(NSString *)user { - _activeResourceUser = user; + [self setORSUser:user]; } - + (NSString *)getPassword { - return _activeResourcePassword; + return [self getORSPassword]; } - + (void)setPassword:(NSString *)password { - _activeResourcePassword = password; -} - -+ (void)setResponseType:(ResponseFormat) format { - _format = format; - switch (format) { - case JSONResponse: - [[self class] setProtocolExtension:@".json"]; - [[self class] setParseDataMethod:@selector(fromJSONData:)]; - [[self class] setSerializeMethod:@selector(toJSONExcluding:)]; - break; - default: - [[self class] setProtocolExtension:@".xml"]; - [[self class] setParseDataMethod:@selector(fromXMLData:)]; - [[self class] setSerializeMethod:@selector(toXMLElementExcluding:)]; - break; - } -} - -+ (ResponseFormat)getResponseType { - return _format; + [self setORSPassword:password]; } - + (SEL)getParseDataMethod { - return (nil == _activeResourceParseDataMethod) ? @selector(fromXMLData:) : _activeResourceParseDataMethod; + return [self getORSParseDataMethod]; } - + (void)setParseDataMethod:(SEL)parseMethod { - _activeResourceParseDataMethod = parseMethod; + [self setORSParseDataMethod:parseMethod]; } - + (SEL) getSerializeMethod { - return (nil == _activeResourceSerializeMethod) ? @selector(toXMLElementExcluding:) : _activeResourceSerializeMethod; + return [self getORSSerializeMethod]; } - + (void) setSerializeMethod:(SEL)serializeMethod { - _activeResourceSerializeMethod = serializeMethod; + [self setORSSerializeMethod:serializeMethod]; } - + (NSString *)protocolExtension { - return _activeResourceProtocolExtension; + return [self getORSProtocolExtension]; } - + (void)setProtocolExtension:(NSString *)protocolExtension { - _activeResourceProtocolExtension = protocolExtension; + [self setORSProtocolExtension:protocolExtension]; } - - -// Find all items -+ (NSArray *)findAllWithResponse:(NSError **)aError { - Response *res = [Connection get:[self collectionPath] withUser:[[self class] getUser] andPassword:[[self class] getPassword]]; - if([res isError] && aError) { - *aError = res.error; - } - return [self performSelector:[self getParseDataMethod] withObject:res.body]; ++ (void)setResponseType:(ORSResponseFormat) format { + [self setORSResponseType:format]; +} ++ (ORSResponseFormat)getResponseType { + return [self getORSResponseType]; } + +// Finders + (NSArray *)findAll { - NSError *aError; - return [self findAllWithResponse:&aError]; + return [self findAllORS]; } - -+ (id)find:(NSString *)elementId withResponse:(NSError **)aError { - Response *res = [Connection get:[self elementPath:elementId] withUser:[[self class] getUser] andPassword:[[self class] getPassword]]; - if([res isError] && aError) { - *aError = res.error; - } - return [self performSelector:[self getParseDataMethod] withObject:res.body]; ++ (NSArray *)findAllWithResponse:(NSError **)aError { + return [self findAllORSWithResponse:aError]; } - + (id)find:(NSString *)elementId { - NSError *aError; - return [self find:elementId withResponse:&aError]; + return [self findORS:elementId]; +} ++ (id)find:(NSString *)elementId withResponse:(NSError **)aError { + return [self findORS:elementId withResponse:aError]; } +// URL construction accessors + (NSString *)elementName { - return [[NSStringFromClass([self class]) stringByReplacingCharactersInRange:NSMakeRange(0, 1) - withString:[[NSStringFromClass([self class]) substringWithRange:NSMakeRange(0,1)] lowercaseString]] underscore]; + return [self getORSElementName]; } - + (NSString *)collectionName { - return [[self elementName] stringByAppendingString:@"s"]; + return [self getORSCollectionName]; } - + (NSString *)elementPath:(NSString *)elementId { - return [NSString stringWithFormat:@"%@%@/%@%@", [self getSite], [self collectionName], elementId, [self protocolExtension]]; + return [self getORSElementPath:elementId]; } - + (NSString *)collectionPath { - return [[[self getSite] stringByAppendingString:[self collectionName]] stringByAppendingString:[self protocolExtension]]; + return [self getORSCollectionPath]; } - + (NSString *)collectionPathWithParameters:(NSDictionary *)parameters { - return [self populatePath:[self collectionPath] withParameters:parameters]; -} - -+ (NSString *)populatePath:(NSString *)path withParameters:(NSDictionary *)parameters { - - // Translate each key to have a preceeding ":" for proper URL param notiation - NSMutableDictionary *parameterized = [NSMutableDictionary dictionaryWithCapacity:[parameters count]]; - for (NSString *key in parameters) { - [parameterized setObject:[parameters objectForKey:key] forKey:[NSString stringWithFormat:@":%@", key]]; - } - return [path gsub:parameterized]; -} - -- (NSString *)collectionPath { - return [[self class] collectionPath]; -} - -// Converts the object to the data format expected by the server -- (NSString *)convertToExpectedType { - // exclude id , created_at , updated_at - NSArray *defaultExclusions = [NSArray arrayWithObjects:[self classIdName],@"createdAt",@"updatedAt",nil]; - return [self performSelector:[[self class] getSerializeMethod] withObject:defaultExclusions]; + return [self getORSCollectionPathWithParameters:parameters]; } - -#pragma mark default equals methods for id and class based equality -- (BOOL)isEqual:(id)anObject { - return [NSStringFromClass([self class]) isEqualToString:NSStringFromClass([anObject class])] && - [anObject respondsToSelector:@selector(getId)] && [[anObject getId] isEqualToString:[self getId]]; -} -- (NSUInteger)hash { - return [[self getId] intValue] + [NSStringFromClass([self class]) hash]; ++ (NSString *)populatePath:(NSString *)path withParameters:(NSDictionary *)parameters { + return [self populateORSPath:path withParameters:parameters]; } -#pragma mark Instance-specific methods +// Instance-specific methods - (id)getId { - id result = nil; - SEL idMethodSelector = NSSelectorFromString([self classIdName]); - if ([self respondsToSelector:idMethodSelector]) { - result = [self performSelector:idMethodSelector]; - } - return result; + return [self getORSId]; } - - (NSString *)classIdName { - - return [NSString stringWithFormat:@"%@Id", - [NSStringFromClass([self class]) stringByReplacingCharactersInRange:NSMakeRange(0, 1) - withString:[[NSStringFromClass([self class]) substringWithRange:NSMakeRange(0,1)] lowercaseString]]]; - -} - -- (BOOL)createAtPath:(NSString *)path withResponse:(NSError **)aError { - Response *res = [Connection post:[self convertToExpectedType] to:path withUser:[[self class] getUser] andPassword:[[self class] getPassword]]; - if([res isError] && aError) { - *aError = res.error; - } - if ([res isSuccess]) { - NSDictionary *newProperties = [[[self class] performSelector:[[self class] getParseDataMethod] withObject:res.body] properties]; - [self setProperties:newProperties]; - return YES; - } - else { - return NO; - } + return [self getORSClassIdName]; } - --(BOOL)updateAtPath:(NSString *)path withResponse:(NSError **)aError { - Response *res = [Connection put:[self convertToExpectedType] to:path - withUser:[[self class] getUser] andPassword:[[self class] getPassword]]; - if([res isError] && aError) { - *aError = res.error; - } - if ([res isSuccess]) { - if([(NSString *)[res.headers objectForKey:@"Content-Length"] intValue] > 1) { - NSDictionary *newProperties = [[[self class] performSelector:[[self class] getParseDataMethod] withObject:res.body] properties]; - [self setProperties:newProperties]; - } - return YES; - } - else { - return NO; - } - -} - -- (BOOL)destroyAtPath:(NSString *)path withResponse:(NSError **)aError { - Response *res = [Connection delete:path withUser:[[self class] getUser] andPassword:[[self class] getPassword]]; - if([res isError] && aError) { - *aError = res.error; - } - return [res isSuccess]; +- (BOOL)create { + return [self createORS]; } - - (BOOL)createWithResponse:(NSError **)aError { - return [self createAtPath:[self collectionPath] withResponse:aError]; + return [self createORSWithResponse:aError]; } - -- (BOOL)create { - NSError *error; - return [self createWithResponse:&error]; +- (BOOL)createWithParameters:(NSDictionary *)parameters { + return [self createORSWithParameters:parameters]; } - - (BOOL)createWithParameters:(NSDictionary *)parameters andResponse:(NSError **)aError { - return [self createAtPath:[[self class] collectionPathWithParameters:parameters] withResponse:aError]; + return [self createORSWithParameters:parameters andResponse:aError]; } - -- (BOOL)createWithParameters:(NSDictionary *)parameters { - NSError *error; - return [self createWithParameters:parameters andResponse:&error]; +- (BOOL)destroy { + return [self destroyORS]; } - - - (BOOL)destroyWithResponse:(NSError **)aError { - id myId = [self getId]; - if (nil != myId) { - return [self destroyAtPath:[[self class] elementPath:myId] withResponse:aError]; - } - else { - // this should return a error - return NO; - } + return [self destroyORSWithResponse:aError]; } - -- (BOOL)destroy { - NSError *error; - return [self destroyWithResponse:&error]; +- (BOOL)update { + return [self updateORS]; } - - (BOOL)updateWithResponse:(NSError **)aError { - id myId = [self getId]; - if (nil != myId) { - return [self updateAtPath:[[self class] elementPath:myId] withResponse:aError]; - } - else { - // this should return an error - return NO; - } + return [self updateORSWithResponse:aError]; } - -- (BOOL)update { - NSError *error; - return [self updateWithResponse:&error]; +- (BOOL)save { + return [self saveORS]; } - - (BOOL)saveWithResponse:(NSError **)aError { - id myId = [self getId]; - if (nil == myId) { - return [self createWithResponse:aError]; - } - else { - return [self updateWithResponse:aError]; - } + return [self saveORSWithResponse:aError]; } - -- (BOOL)save { - NSError *error; - return [self saveWithResponse:&error]; +- (BOOL)createAtPath:(NSString *)path withResponse:(NSError **)aError { + return [self createORSAtPath:path withResponse:aError]; } - - -- (void) dealloc -{ - [super dealloc]; +- (BOOL)updateAtPath:(NSString *)path withResponse:(NSError **)aError { + return [self updateORSAtPath:path withResponse:aError]; +} +- (BOOL)destroyAtPath:(NSString *)path withResponse:(NSError **)aError { + return [self destroyORSAtPath:path withResponse:aError]; } - - @end \ No newline at end of file diff --git a/Classes/lib/objective_support b/Classes/lib/objective_support index a7476d6..0259253 160000 --- a/Classes/lib/objective_support +++ b/Classes/lib/objective_support @@ -1 +1 @@ -Subproject commit a7476d6f2e9e168b6daabcc6d2b5e23f89675c01 +Subproject commit 0259253513ff12451f1d0cfea08b5daa7afe62d7 diff --git a/objective_resource.xcodeproj/project.pbxproj b/objective_resource.xcodeproj/project.pbxproj index dc30158..8bd9d72 100644 --- a/objective_resource.xcodeproj/project.pbxproj +++ b/objective_resource.xcodeproj/project.pbxproj @@ -62,6 +62,7 @@ 357A91B60E9A55EF0025D9AF /* Connection.m in Sources */ = {isa = PBXBuildFile; fileRef = 357A919B0E9A55EF0025D9AF /* Connection.m */; }; 357A91C00E9A55EF0025D9AF /* Response.m in Sources */ = {isa = PBXBuildFile; fileRef = 357A91B00E9A55EF0025D9AF /* Response.m */; }; 3584DFD60F31F684008E596B /* NSObject+ObjectiveResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 3584DFD50F31F684008E596B /* NSObject+ObjectiveResource.m */; }; + 3584E0A40F32334D008E596B /* NSObject+ObjectiveResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 3584DFD50F31F684008E596B /* NSObject+ObjectiveResource.m */; }; 35C679300F2E70C2005FF6CE /* NSString+InflectionSupport.m in Sources */ = {isa = PBXBuildFile; fileRef = 35C678FC0F2E70C2005FF6CE /* NSString+InflectionSupport.m */; }; 35C679310F2E70C2005FF6CE /* NSData+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = 35C678FE0F2E70C2005FF6CE /* NSData+Additions.m */; }; 35C679320F2E70C2005FF6CE /* NSObject+PropertySupport.m in Sources */ = {isa = PBXBuildFile; fileRef = 35C679000F2E70C2005FF6CE /* NSObject+PropertySupport.m */; }; @@ -622,6 +623,7 @@ 357A91B60E9A55EF0025D9AF /* Connection.m in Sources */, 357A91C00E9A55EF0025D9AF /* Response.m in Sources */, 23829E0C0EA393700070F0BF /* EditDogViewController.m in Sources */, + 3584E0A40F32334D008E596B /* NSObject+ObjectiveResource.m in Sources */, 23829F010EA39B3B0070F0BF /* ViewDogController.m in Sources */, 2377C4F50F019E67006E155F /* NSHTTPURLResponse+Error.m in Sources */, 23C922F40F25066900EDE8AF /* NSMutableURLRequest+ResponseType.m in Sources */,