Navigation Menu

Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
David Cordero committed Feb 4, 2014
2 parents 22712f9 + bc2a670 commit 1a6be4e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Configuration
2 changes: 1 addition & 1 deletion LICENSE.md
@@ -1,4 +1,4 @@
**Copyright (c) 2012 - 2013, GitHub, Inc.**
**Copyright (c) 2012 - 2014, GitHub, Inc.**
**All rights reserved.**

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Expand Down
2 changes: 1 addition & 1 deletion Mantle/MTLJSONAdapter.m
Expand Up @@ -15,7 +15,7 @@
const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary = 3;

// An exception was thrown and caught.
static const NSInteger MTLJSONAdapterErrorExceptionThrown = 1;
const NSInteger MTLJSONAdapterErrorExceptionThrown = 1;

// Associated with the NSException that was caught.
static NSString * const MTLJSONAdapterThrownExceptionErrorKey = @"MTLJSONAdapterThrownException";
Expand Down
11 changes: 4 additions & 7 deletions Mantle/MTLManagedObjectAdapter.m
Expand Up @@ -34,9 +34,6 @@ static id performInContext(NSManagedObjectContext *context, id (^block)(void)) {
return result;
}

// An exception was thrown and caught.
static const NSInteger MTLManagedObjectAdapterErrorExceptionThrown = 1;

@interface MTLManagedObjectAdapter ()

// The MTLModel subclass being serialized or deserialized.
Expand Down Expand Up @@ -308,7 +305,7 @@ - (id)managedObjectFromModel:(MTLModel<MTLManagedObjectSerializing> *)model inse
Class fetchRequestClass = NSClassFromString(@"NSFetchRequest");
NSAssert(fetchRequestClass != nil, @"CoreData.framework must be linked to use MTLManagedObjectAdapter");

// If a uniquing predicate is provided, perform a fetch request to guarentee a unique managed object.
// If a uniquing predicate is provided, perform a fetch request to guarantee a unique managed object.
__block NSManagedObject *managedObject = nil;
NSPredicate *uniquingPredicate = [self uniquingPredicateForModel:model];

Expand Down Expand Up @@ -392,7 +389,7 @@ - (id)managedObjectFromModel:(MTLModel<MTLManagedObjectSerializing> *)model inse
NSValueTransformer *attributeTransformer = [self entityAttributeTransformerForKey:propertyKey];
if (attributeTransformer != nil) transformedValue = [attributeTransformer transformedValue:transformedValue];

if (![managedObject validateValue:&transformedValue forKey:managedObjectKey error:error]) return NO;
if (![managedObject validateValue:&transformedValue forKey:managedObjectKey error:&tmpError]) return NO;
[managedObject setValue:transformedValue forKey:managedObjectKey];

return YES;
Expand Down Expand Up @@ -502,8 +499,8 @@ - (id)managedObjectFromModel:(MTLModel<MTLManagedObjectSerializing> *)model inse
}
}];

if (managedObject != nil && ![managedObject validateForInsert:error]) {
performInContext(context, ^ id {
if (managedObject != nil && ![managedObject validateForInsert:&tmpError]) {
managedObject = performInContext(context, ^ id {
[context deleteObject:managedObject];
return nil;
});
Expand Down
Expand Up @@ -121,7 +121,7 @@ + (NSValueTransformer *)mtl_valueMappingTransformerWithDictionary:(NSDictionary
NSParameterAssert(dictionary.count == [[NSSet setWithArray:dictionary.allValues] count]);

return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(id<NSCopying> key) {
return dictionary[key];
return dictionary[key ?: NSNull.null];
} reverseBlock:^(id object) {
__block id result = nil;
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id anObject, BOOL *stop) {
Expand Down
5 changes: 5 additions & 0 deletions MantleTests/MTLCoreDataTestModels.h
Expand Up @@ -23,6 +23,11 @@

@end

// Model for Parent entity which doesn't serialize required properties
@interface MTLParentIncorrectTestModel : MTLModel <MTLManagedObjectSerializing>

@end

// Corresponds to the `Child` entity.
@interface MTLChildTestModel : MTLModel <MTLManagedObjectSerializing>

Expand Down
12 changes: 12 additions & 0 deletions MantleTests/MTLCoreDataTestModels.m
Expand Up @@ -42,6 +42,18 @@ + (NSDictionary *)relationshipModelClassesByPropertyKey {

@end

@implementation MTLParentIncorrectTestModel

+ (NSString *)managedObjectEntityName {
return @"Parent";
}

+ (NSDictionary *)managedObjectKeysByPropertyKey {
return @{};
}

@end

@implementation MTLChildTestModel

+ (NSString *)managedObjectEntityName {
Expand Down
20 changes: 20 additions & 0 deletions MantleTests/MTLManagedObjectAdapterSpec.m
Expand Up @@ -216,6 +216,26 @@
expect(error).notTo.beNil();
});

it(@"should return an error if model doesn't validate for attribute description", ^{
MTLParentTestModel *parentModel = [MTLParentTestModel modelWithDictionary:@{} error:NULL];

NSError *error;
NSManagedObject *managedObject = [MTLManagedObjectAdapter managedObjectFromModel:parentModel insertingIntoContext:context error:&error];

expect(managedObject).to.beNil();
expect(error).notTo.beNil();
});

it(@"should return an error if model doesn't validate for insert", ^{
MTLParentIncorrectTestModel *parentModel = [MTLParentIncorrectTestModel modelWithDictionary:@{} error:NULL];

NSError *error;
NSManagedObject *managedObject = [MTLManagedObjectAdapter managedObjectFromModel:parentModel insertingIntoContext:context error:&error];

expect(managedObject).to.beNil();
expect(error).notTo.beNil();
});

it(@"should respect the uniqueness constraint", ^{
NSError *errorOne;
MTLParent *parentOne = [MTLManagedObjectAdapter managedObjectFromModel:parentModel insertingIntoContext:context error:&errorOne];
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -272,7 +272,7 @@ be invoked if overridden, giving you a convenient hook to upgrade old data.
In order to serialize your model objects from or into JSON, you need to
implement `<MTLJSONSerializing>` in your `MTLModel` subclass. This allows you to
use `MTLJSONAdapter` convert your model objects from JSON and back:
use `MTLJSONAdapter` to convert your model objects from JSON and back:
```objc
NSError *error = nil;
Expand Down

0 comments on commit 1a6be4e

Please sign in to comment.