Skip to content

Commit

Permalink
Merge pull request #311 from kilink/master
Browse files Browse the repository at this point in the history
make mergeValuesForKeysFromModel: work with sub classes
  • Loading branch information
robb committed Apr 24, 2014
2 parents 95c4477 + 272fde5 commit 175ec19
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Mantle/MTLModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ - (void)mergeValueForKey:(NSString *)key fromModel:(MTLModel *)model {
}

- (void)mergeValuesForKeysFromModel:(MTLModel *)model {
NSSet *propertyKeys = model.class.propertyKeys;
for (NSString *key in self.class.propertyKeys) {
if (![propertyKeys containsObject:key]) continue;

[self mergeValueForKey:key fromModel:model];
}
}
Expand Down
39 changes: 39 additions & 0 deletions MantleTests/MTLModelSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,43 @@
expect(target.count).to.equal(8);
});

describe(@"merging with model subclasses", ^{
__block MTLTestModel *superclass;
__block MTLSubclassTestModel *subclass;

beforeEach(^{
superclass = [MTLTestModel modelWithDictionary:@{
@"name": @"foo",
@"count": @5
} error:NULL];

expect(superclass).notTo.beNil();

subclass = [MTLSubclassTestModel modelWithDictionary:@{
@"name": @"bar",
@"count": @3,
@"generation": @1,
@"role": @"subclass"
} error:NULL];

expect(subclass).notTo.beNil();
});

it(@"should merge from subclass model", ^{
[superclass mergeValuesForKeysFromModel:subclass];

expect(superclass.name).to.equal(@"bar");
expect(superclass.count).to.equal(8);
});

it(@"should merge from superclass model", ^{
[subclass mergeValuesForKeysFromModel:superclass];

expect(subclass.name).to.equal(@"foo");
expect(subclass.count).to.equal(8);
expect(subclass.generation).to.equal(1);
expect(subclass.role).to.equal(@"subclass");
});
});

SpecEnd
8 changes: 8 additions & 0 deletions MantleTests/MTLTestModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ extern const NSInteger MTLTestModelNameMissing;

@end

@interface MTLSubclassTestModel : MTLTestModel

// Properties to test merging between subclass and superclass
@property (nonatomic, copy) NSString *role;
@property (nonatomic, copy) NSNumber *generation;

@end

@interface MTLArrayTestModel : MTLModel <MTLJSONSerializing>

// This property is associated with a "users.username" key in JSON.
Expand Down
3 changes: 3 additions & 0 deletions MantleTests/MTLTestModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ - (void)mergeCountFromModel:(MTLTestModel *)model {

@end

@implementation MTLSubclassTestModel
@end

@implementation MTLArrayTestModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
Expand Down

0 comments on commit 175ec19

Please sign in to comment.