Skip to content

support for more advanced relationships #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperation
NSError *err;

//Using categories doesn't require additional setups
User *user = [User insertWithDictionary:JSON error:&err];
User *user = [User insertWithDictionary:JSON error:&err commit:YES];];

//you are good to go with `user`

Expand Down
20 changes: 20 additions & 0 deletions Sample/Address.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Address.h
// Sample
//
// Created by amir hayek on 1/27/14.
// Copyright (c) 2014 Jaehwa Han. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Contact;

@interface Address : NSManagedObject

@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * state;
@property (nonatomic, retain) Contact *contact;

@end
19 changes: 19 additions & 0 deletions Sample/Address.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Address.m
// Sample
//
// Created by amir hayek on 1/27/14.
// Copyright (c) 2014 Jaehwa Han. All rights reserved.
//

#import "Address.h"
#import "Contact.h"


@implementation Address

@dynamic city;
@dynamic state;
@dynamic contact;

@end
29 changes: 29 additions & 0 deletions Sample/Contact.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Contact.h
// Sample
//
// Created by amir hayek on 1/27/14.
// Copyright (c) 2014 Jaehwa Han. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Address, PhoneNumber;

@interface Contact : NSManagedObject

@property (nonatomic, retain) NSNumber * age;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Address *address;
@property (nonatomic, retain) NSSet *phoneNumbers;
@end

@interface Contact (CoreDataGeneratedAccessors)

- (void)addPhoneNumbersObject:(PhoneNumber *)value;
- (void)removePhoneNumbersObject:(PhoneNumber *)value;
- (void)addPhoneNumbers:(NSSet *)values;
- (void)removePhoneNumbers:(NSSet *)values;

@end
21 changes: 21 additions & 0 deletions Sample/Contact.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Contact.m
// Sample
//
// Created by amir hayek on 1/27/14.
// Copyright (c) 2014 Jaehwa Han. All rights reserved.
//

#import "Contact.h"
#import "Address.h"
#import "PhoneNumber.h"


@implementation Contact

@dynamic age;
@dynamic name;
@dynamic address;
@dynamic phoneNumbers;

@end
4 changes: 2 additions & 2 deletions Sample/Sample/DataVersion.h → Sample/DataVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// DataVersion.h
// Sample
//
// Created by Jaehwa Han on 12/4/12.
// Copyright (c) 2012 Jaehwa Han. All rights reserved.
// Created by amir hayek on 1/27/14.
// Copyright (c) 2014 Jaehwa Han. All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions Sample/Sample/DataVersion.m → Sample/DataVersion.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// DataVersion.m
// Sample
//
// Created by Jaehwa Han on 12/4/12.
// Copyright (c) 2012 Jaehwa Han. All rights reserved.
// Created by amir hayek on 1/27/14.
// Copyright (c) 2014 Jaehwa Han. All rights reserved.
//

#import "DataVersion.h"
Expand Down
6 changes: 3 additions & 3 deletions Sample/Sample/Group.h → Sample/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Group.h
// Sample
//
// Created by Jaehwa Han on 12/4/12.
// Copyright (c) 2012 Jaehwa Han. All rights reserved.
// Created by amir hayek on 1/27/14.
// Copyright (c) 2014 Jaehwa Han. All rights reserved.
//

#import <Foundation/Foundation.h>
Expand All @@ -12,8 +12,8 @@

@interface Group : NSManagedObject

@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSDate * createdAt;
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * title;

@end
6 changes: 3 additions & 3 deletions Sample/Sample/Group.m → Sample/Group.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
// Group.m
// Sample
//
// Created by Jaehwa Han on 12/4/12.
// Copyright (c) 2012 Jaehwa Han. All rights reserved.
// Created by amir hayek on 1/27/14.
// Copyright (c) 2014 Jaehwa Han. All rights reserved.
//

#import "Group.h"


@implementation Group

@dynamic title;
@dynamic createdAt;
@dynamic id;
@dynamic title;

@end
40 changes: 33 additions & 7 deletions Sample/NSManagedObject Goodies Test/NSManagedObject_Goodies_Test.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (void)testExample
////
NSDictionary *user = @{@"name": @"John Doe", @"emails":@[@"[email protected]", @"[email protected]"]};
NSError *err;
User *u = [User insertWithDictionary:user error:&err];
User *u = [User newWithDictionary:user error:&err commit:YES];
STAssertNil(err, @"There should be no error");
STAssertTrue(u != nil, @"User must be persisted");

Expand All @@ -74,7 +74,7 @@ - (void)testExample
//// Key format conversion (under_score and camelCase) & Data conversion (NSDate)
////
NSDictionary *group = @{@"title":@"My best friends", @"created_at":@(1354652336)};
Group *g = [Group insertWithDictionary:group error:&err];
Group *g = [Group newWithDictionary:group error:&err commit:YES];
STAssertNil(err, @"There should be no error");
STAssertNotNil(g, @"New group should not be nil");
STAssertTrue([[Group findWithPredicate:nil] count] == 1, @"");
Expand All @@ -89,7 +89,7 @@ - (void)testExample
////

NSDictionary *group2 = @{@"title":group[@"title"], @"created_at":@(1354653107)};
Group *g2 = [Group insertWithDictionary:group2 uniqueKey:@"title" error:&err];
Group *g2 = [Group newWithDictionary:group2 uniqueKey:@"title" error:&err commit:YES];
STAssertNil(g2, @"Duplicate key records can't be exist simultaneously");

NSArray *groups = [Group findWithPredicate:nil];
Expand All @@ -99,10 +99,10 @@ - (void)testExample
//// Default unique key test (`id` or `_id`)
////
NSDictionary *group3 = @{@"title":@"Group 3", @"id":@(42), @"created_at":@(1354653107)};
Group *g3 = [Group updateWithDictionary:group3];
Group *g3 = [Group updateWithDictionary:group3 commit:YES];

NSDictionary *group4 = @{@"title":@"Group 4", @"id":@(42), @"created_at":@(1354653108)};
Group *g4 = [Group updateWithDictionary:group4];
Group *g4 = [Group updateWithDictionary:group4 commit:YES];

STAssertTrue([g4.title isEqualToString:group4[@"title"]], @"updated comparing id");

Expand All @@ -113,10 +113,10 @@ - (void)testExample
//// Multi key test (these conditions are concatenated by `AND`)
////
NSDictionary *group5 = @{@"title":@"Group 5", @"id":@(43), @"created_at":@(1354653107)};
Group *g5 = [Group updateWithDictionary:group5 uniqueKeys:@[@"id", @"title"] upsert:YES error:nil];
Group *g5 = [Group updateWithDictionary:group5 uniqueKeys:@[@"id", @"title"] upsert:YES error:nil commit:YES];

NSDictionary *group6 = @{@"title":@"Group 6", @"id":@(43), @"created_at":@(1354653108)};
Group *g6 = [Group updateWithDictionary:group6 uniqueKeys:@[@"id", @"title"] upsert:YES error:nil];
Group *g6 = [Group updateWithDictionary:group6 uniqueKeys:@[@"id", @"title"] upsert:YES error:nil commit:YES];

STAssertTrue([[Group findWithPredicate:@"SELF.id == 43"] count] == 2, @"Multikey should be concatenated with AND");

Expand All @@ -131,4 +131,30 @@ - (void)testExample
STAssertEqualObjects(g2.title, @"friends", @"group must be updated");
}

-(void)testContact
{
NSDictionary* contactDict = @{@"name": @"John Smith",
@"age": @25 ,
@"address":@{
@"city": @"New York",
@"state": @"NY"},
@"phoneNumbers":@[
@{@"type": @"home",
@"number": @3454353},
@{@"type": @"fax",
@"number": @1235},
]
};

NSError *err;
Contact* contact = [Contact newWithDictionary:contactDict error:&err commit:YES];
STAssertNil(err, @"There should be no error");
STAssertTrue(contact != nil, @"User must be persisted");

STAssertTrue([contact.address.city isEqualToString:@"New York"], @"Wrong address");
STAssertTrue(contact.phoneNumbers.count == 2, @"A phone number missing");
}



@end
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

#import <Foundation/Foundation.h>

@interface NSString (CamelCaseConversion)
@interface NSString (StringTools)

- (NSString *)toCamelCase;
- (NSString *)toUnderscore;
- (NSString *) capitalizeFirstLetter;
- (NSString *) lowercaseFirstLetter;
- (NSString *) humanize;
- (NSString*)pluralToSingle;
- (BOOL)isVowel;

@end
113 changes: 113 additions & 0 deletions Sample/NSString+StringTools.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// NSString+CamelCaseConversion.m
// Sample
//
// Created by Jaehwa Han on 12/4/12.
// Copyright (c) 2012 Jaehwa Han. All rights reserved.
//

#import "NSString+StringTools.h"
#import "RegexKitLite.h"

@implementation NSString (StringTools)
- (NSString *)toCamelCase
{
return [self stringByReplacingOccurrencesOfRegex:@"_([a-zA-Z0-9]+)" usingBlock:^NSString *(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop) {
return [capturedStrings[1] capitalizedString];
}];
}

- (NSString *)toUnderscore
{
return [self stringByReplacingOccurrencesOfRegex:@"[A-Z]" usingBlock:^NSString *(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop) {
unichar cs[2] = {(unichar)'_', [capturedStrings[0] characterAtIndex:0] + 32};
return [NSString stringWithCharacters:cs length:2];
}];
}


- (NSString *) capitalizeFirstLetter
{
return [NSString stringWithFormat:@"%@%@",
[[self substringWithRange:NSMakeRange(0, 1)] uppercaseString],
[self substringWithRange:NSMakeRange(1, [self length]-1)]
];
}


- (NSString *) capitalizeLastLetter
{
return [NSString stringWithFormat:@"%@%@",
[self substringWithRange:NSMakeRange(0, [self length]-1)],
[[self substringWithRange:NSMakeRange([self length]-1, 1)] uppercaseString]
];
}

- (NSString *) humanize
{
return [self stringByReplacingOccurrencesOfRegex:@"([a-z])([A-Z])" withString:@"$1 $2"];
}

- (NSString *) lowercaseFirstLetter
{
return [NSString stringWithFormat:@"%@%@",
[[self substringWithRange:NSMakeRange(0, 1)] lowercaseString],
[self substringWithRange:NSMakeRange(1, [self length]-1)]
];
}

-(NSString*)pluralToSingle
{
if ([self pluralToSingleExceptions]){
return [self pluralToSingleExceptions];
}

if ([[self substringFromIndex:self.length-3] isEqualToString:@"ies"]) { //e.g. countries
return [NSString stringWithFormat:@"%@y",[self substringWithRange:NSMakeRange(0, self.length-3)]];
}
if ([[self substringFromIndex:self.length-2] isEqualToString:@"es"]) {
if (
[[self substringWithRange:NSMakeRange(self.length-3, 1)] isEqualToString:@"x"] || //e.g. boxes
[[self substringWithRange:NSMakeRange(self.length-3, 1)] isEqualToString:@"s"] || //e.g. kisses
[[self substringWithRange:NSMakeRange(self.length-4, 2)] isEqualToString:@"ch"] || //e.g. churches
[[self substringWithRange:NSMakeRange(self.length-4, 2)] isEqualToString:@"sh"] //e.g. dishes
)
{
return [self substringWithRange:NSMakeRange(0, [self length]-2)];
}
}

if ([[self substringFromIndex:self.length-1] isEqualToString:@"s"]) //e.g. dogs, books, towns
{
return [self substringWithRange:NSMakeRange(0, [self length]-1)];
}

return self;
}

-(NSString*)pluralToSingleExceptions
{
if ([self isEqualToString:@"taxies"]) {
return @"taxi";
}

return nil;
}

-(BOOL)isVowel
{
if ([self isEqualToString:@"a"] ||
[self isEqualToString:@"e"] ||
[self isEqualToString:@"i"] ||
[self isEqualToString:@"o"] ||
[self isEqualToString:@"u"] )
{
return YES;
}

return NO;
}



@end
Loading