|
18 | 18 |
|
19 | 19 | @implementation PFRole
|
20 | 20 |
|
21 |
| -#pragma mark Creating a New Role |
| 21 | +///-------------------------------------- |
| 22 | +#pragma mark - Init |
| 23 | +///-------------------------------------- |
22 | 24 |
|
23 | 25 | - (instancetype)initWithName:(NSString *)name {
|
24 | 26 | return [self initWithName:name acl:nil];
|
@@ -50,40 +52,29 @@ + (instancetype)roleWithName:(NSString *)name acl:(PFACL *)acl {
|
50 | 52 |
|
51 | 53 | // Dynamic synthesizers would use objectForKey, not relationForKey
|
52 | 54 | - (PFRelation *)roles {
|
53 |
| - return [self relationForKey:@"roles"]; |
| 55 | + return [self relationForKey:@keypath(PFRole, roles)]; |
54 | 56 | }
|
55 | 57 |
|
56 | 58 | - (PFRelation *)users {
|
57 |
| - return [self relationForKey:@"users"]; |
| 59 | + return [self relationForKey:@keypath(PFRole, users)]; |
58 | 60 | }
|
59 | 61 |
|
60 | 62 | ///--------------------------------------
|
61 | 63 | #pragma mark - PFObject Overrides
|
62 | 64 | ///--------------------------------------
|
63 | 65 |
|
64 | 66 | - (void)setObject:(id)object forKey:(NSString *)key {
|
65 |
| - if ([@"name" isEqualToString:key]) { |
66 |
| - if (self.objectId) { |
67 |
| - [NSException raise:NSInternalInconsistencyException |
68 |
| - format:@"A role's name can only be set before it has been saved."]; |
69 |
| - } |
70 |
| - if (![object isKindOfClass:[NSString class]]) { |
71 |
| - [NSException raise:NSInvalidArgumentException |
72 |
| - format:@"A role's name must be an NSString."]; |
73 |
| - } |
74 |
| - if ([object rangeOfString:@"^[0-9a-zA-Z_\\- ]+$" options:NSRegularExpressionSearch].location == NSNotFound) { |
75 |
| - [NSException raise:NSInvalidArgumentException |
76 |
| - format:@"A role's name can only contain alphanumeric characters, _, -, and spaces."]; |
77 |
| - } |
| 67 | + if ([key isEqualToString:@keypath(PFRole, name)]) { |
| 68 | + PFConsistencyAssert(!self.objectId, @"A role's name can only be set before it has been saved."); |
| 69 | + PFParameterAssert([object isKindOfClass:[NSString class]], @"A role's name must be an NSString."); |
| 70 | + PFParameterAssert([object rangeOfString:@"^[0-9a-zA-Z_\\- ]+$" options:NSRegularExpressionSearch].location != NSNotFound, |
| 71 | + @"A role's name can only contain alphanumeric characters, _, -, and spaces."); |
78 | 72 | }
|
79 | 73 | [super setObject:object forKey:key];
|
80 | 74 | }
|
81 | 75 |
|
82 | 76 | - (BFTask *)saveInBackground {
|
83 |
| - if (!self.objectId && !self.name) { |
84 |
| - [NSException raise:NSInternalInconsistencyException |
85 |
| - format:@"New roles must specify a name."]; |
86 |
| - } |
| 77 | + PFConsistencyAssert(self.objectId || self.name, @"New roles must specify a name."); |
87 | 78 | return [super saveInBackground];
|
88 | 79 | }
|
89 | 80 |
|
|
0 commit comments