Skip to content

Commit

Permalink
Merge pull request Mantle#568 from jmah/jmah/protocol-storage
Browse files Browse the repository at this point in the history
Check superclass for property persistence
  • Loading branch information
robb committed Jul 31, 2015
2 parents d41e8d3 + 0747420 commit 1b37d35
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
14 changes: 10 additions & 4 deletions Mantle/MTLModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,18 @@ + (MTLPropertyStorage)storageBehaviorForPropertyWithKey:(NSString *)propertyKey
free(attributes);
};

Method getterMethod = class_getInstanceMethod(self.class, attributes->getter);
Method setterMethod = class_getInstanceMethod(self.class, attributes->setter);
if (!attributes->dynamic && attributes->ivar == NULL && getterMethod == NULL && setterMethod == NULL) {
BOOL hasGetter = [self instancesRespondToSelector:attributes->getter];
BOOL hasSetter = [self instancesRespondToSelector:attributes->setter];
if (!attributes->dynamic && attributes->ivar == NULL && !hasGetter && !hasSetter) {
return MTLPropertyStorageNone;
} else if (attributes->readonly && attributes->ivar == NULL) {
return MTLPropertyStorageNone;
if ([self isEqual:MTLModel.class]) {
return MTLPropertyStorageNone;
} else {
// Check superclass in case the subclass redeclares a property that
// falls through
return [self.superclass storageBehaviorForPropertyWithKey:propertyKey];
}
} else {
return MTLPropertyStoragePermanent;
}
Expand Down
14 changes: 10 additions & 4 deletions MantleTests/MTLModelSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@
expect(@([MTLStorageBehaviorModel storageBehaviorForPropertyWithKey:@"notIvarBacked"])).to(equal(@(MTLPropertyStorageNone)));
});

it(@"should consider properties declared in subclass with storage in superclass permanent", ^{
expect(@([MTLStorageBehaviorModelSubclass storageBehaviorForPropertyWithKey:@"shadowedInSubclass"])).to(equal(@(MTLPropertyStoragePermanent)));
expect(@([MTLStorageBehaviorModelSubclass storageBehaviorForPropertyWithKey:@"declaredInProtocol"])).to(equal(@(MTLPropertyStoragePermanent)));
});

it(@"should ignore optional protocol properties not implemented", ^{
expect(@([MTLOptionalPropertyModel storageBehaviorForPropertyWithKey:@"optionalUnimplementedProperty"])).to(equal(@(MTLPropertyStorageNone)));
expect(@([MTLOptionalPropertyModel storageBehaviorForPropertyWithKey:@"optionalImplementedProperty"])).to(equal(@(MTLPropertyStoragePermanent)));
});

describe(@"merging with model subclasses", ^{
__block MTLTestModel *superclass;
__block MTLSubclassTestModel *subclass;
Expand Down Expand Up @@ -190,9 +200,5 @@
});
});

it(@"should ignore optional protocol properties not implemented", ^{
expect(@([MTLOptionalPropertyModel storageBehaviorForPropertyWithKey:@"optionalProperty"])).to(equal(@(MTLPropertyStorageNone)));
});


QuickSpecEnd
20 changes: 19 additions & 1 deletion MantleTests/MTLTestModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ extern const NSInteger MTLTestModelNameMissing;
@property (readonly, nonatomic, weak) id weakProperty;
@property (readonly, nonatomic, strong) id strongProperty;

@property (readonly, nonatomic, strong) id shadowedInSubclass;
@property (readonly, nonatomic, strong) id declaredInProtocol;

@end

@protocol MTLDateProtocol <NSObject>

@property (readonly, nonatomic, strong) id declaredInProtocol;

@end

@interface MTLStorageBehaviorModelSubclass : MTLStorageBehaviorModel <MTLDateProtocol>

@property (readonly, nonatomic, strong) id shadowedInSubclass;

@end

@interface MTLBoolModel : MTLModel <MTLJSONSerializing>
Expand Down Expand Up @@ -159,10 +174,13 @@ extern const NSInteger MTLTestModelNameMissing;
@protocol MTLOptionalPropertyProtocol

@optional
@property (readwrite, nonatomic, copy) NSString *optionalProperty;
@property (readwrite, nonatomic, strong) id optionalUnimplementedProperty;
@property (readwrite, nonatomic, strong) id optionalImplementedProperty;

@end

@interface MTLOptionalPropertyModel : MTLModel <MTLOptionalPropertyProtocol>

@property (readwrite, nonatomic, strong) id optionalImplementedProperty;

@end
6 changes: 6 additions & 0 deletions MantleTests/MTLTestModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ - (id)notIvarBacked {

@end

@implementation MTLStorageBehaviorModelSubclass

@dynamic shadowedInSubclass;

@end

@implementation MTLMultiKeypathModel

#pragma mark MTLJSONSerializing
Expand Down

0 comments on commit 1b37d35

Please sign in to comment.