Skip to content

Commit

Permalink
expand ignoreMissingFields property to work with CoreData. Bump podspec.
Browse files Browse the repository at this point in the history
  • Loading branch information
DenTelezhkin committed Mar 1, 2016
1 parent b6ec3d8 commit ac658bc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion EasyMapping.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "EasyMapping"
s.version = "0.15.5"
s.version = "0.16.0"
s.summary = "The easiest way to map data from your webservice."
s.homepage = "https://github.com/lucasmedeirosleite/EasyMapping"

Expand Down
22 changes: 16 additions & 6 deletions EasyMapping/EKManagedObjectMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,26 @@ - (id)fillObject:(id)object fromExternalRepresentation:(NSDictionary *)externalR
[mapping.hasOneMappings enumerateKeysAndObjectsUsingBlock:^(id key, EKRelationshipMapping * relationship, BOOL * stop)
{
NSDictionary * value = [relationship extractObjectFromRepresentation:representation];
if (value)
if(mapping.ignoreMissingFields && (!value || value == (id)[NSNull null]))
{
id result = nil;
if (value != (id)[NSNull null]) {
result = [self objectFromExternalRepresentation:value withMapping:(EKManagedObjectMapping *)[relationship objectMapping]];
}
[EKPropertyHelper setValue:result onObject:object forKeyPath:relationship.property];
return;
}
if (value && value != (id)[NSNull null]) {
id result = [self objectFromExternalRepresentation:value withMapping:(EKManagedObjectMapping *)[relationship objectMapping]];
[EKPropertyHelper setValue:result onObject:object forKeyPath:relationship.property];
} else {
[EKPropertyHelper setValue:nil onObject:object forKeyPath:relationship.property];
}

}];
[mapping.hasManyMappings enumerateKeysAndObjectsUsingBlock:^(id key, EKRelationshipMapping * relationship, BOOL * stop)
{
NSArray * arrayToBeParsed = [representation valueForKeyPath:key];
if(mapping.ignoreMissingFields && (!arrayToBeParsed || arrayToBeParsed == (id)[NSNull null]))
{
return;
}

if (arrayToBeParsed && arrayToBeParsed != (id)[NSNull null])
{
NSArray * parsedArray = [self arrayOfObjectsFromExternalRepresentation:arrayToBeParsed
Expand All @@ -102,6 +110,8 @@ - (id)fillObject:(id)object fromExternalRepresentation:(NSDictionary *)externalR
else {
[EKPropertyHelper setValue:parsedObjects onObject:object forKeyPath:relationship.property];
}
} else {
[EKPropertyHelper setValue:nil onObject:object forKeyPath:relationship.property];
}
}];
return object;
Expand Down
2 changes: 1 addition & 1 deletion EasyMapping/EKMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ + (id)fillObject:(id)object fromExternalRepresentation:(NSDictionary *)externalR
[mapping.hasOneMappings enumerateKeysAndObjectsUsingBlock:^(id key, EKRelationshipMapping * valueMapping, BOOL *stop) {
NSDictionary * value = [valueMapping extractObjectFromRepresentation:representation];

if(mapping.ignoreMissingFields && !value)
if(mapping.ignoreMissingFields && ((!value) || (value == (id)[NSNull null])))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
9A53E2E61A7EA7F200C4D169 /* EKMappingBlocksSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A53E2E51A7EA7F200C4D169 /* EKMappingBlocksSpec.m */; };
9A53E2E71A7EA7F200C4D169 /* EKMappingBlocksSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A53E2E51A7EA7F200C4D169 /* EKMappingBlocksSpec.m */; };
9A7048131AB464A700EFD403 /* CarWithAttributesRemoved.json in Resources */ = {isa = PBXBuildFile; fileRef = 9EC0BF2C1AB35A1D00F584DF /* CarWithAttributesRemoved.json */; };
9A74F5401C85F2CE0063247E /* PersonWithoutRelations.json in Resources */ = {isa = PBXBuildFile; fileRef = 4D612CB51C85DB8C00A167ED /* PersonWithoutRelations.json */; };
9A8420DC19570689006DDDE3 /* BaseManagedTestModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A2056A41956E0EE00499FC3 /* BaseManagedTestModel.m */; };
9A8420DD1957068A006DDDE3 /* BaseManagedTestModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A2056A41956E0EE00499FC3 /* BaseManagedTestModel.m */; };
9A8420DE1957068D006DDDE3 /* BaseTestModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A2056A01956DF7900499FC3 /* BaseTestModel.m */; };
Expand Down Expand Up @@ -1309,6 +1310,7 @@
9A210F3B1944B81500871071 /* PersonRecursive.json in Resources */,
9A45122819164705006E6022 /* CarWithNestedAttributes.json in Resources */,
9A45124419164705006E6022 /* PersonWithDifferentNaming.json in Resources */,
9A74F5401C85F2CE0063247E /* PersonWithoutRelations.json in Resources */,
9AC0D8691A728D4C003926C1 /* PersonNonNested.json in Resources */,
9A45125019164705006E6022 /* Plane.json in Resources */,
9A45123C19164705006E6022 /* NativeChild.json in Resources */,
Expand Down
54 changes: 54 additions & 0 deletions EasyMappingExample/Tests/Specs/EKManagedObjectMappingSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
#import "EasyMapping.h"
#import "ManagedPerson.h"
#import "ManagedCar.h"
#import "ManagedPhone.h"
#import "ManagedMappingProvider.h"
#import <CoreData/CoreData.h>
#import "CMFixture.h"
#import <MagicalRecord/MagicalRecord.h>
#import <MagicalRecord/MagicalRecord+Setup.h>
#import <MagicalRecord/NSManagedObjectContext+MagicalSaves.h>
#import <MagicalRecord/NSManagedObject+MagicalFinders.h>
#import <MagicalRecord/NSManagedObject+MagicalRecord.h>

SPEC_BEGIN(EKManagedObjectMappingSpec)

Expand Down Expand Up @@ -385,6 +392,53 @@

});


describe(@"Supports ignore missing fields property", ^{

__block NSDictionary *externalRepresentation;
__block NSDictionary *externalRepresentationPartial;
__block EKManagedObjectMapping * personMapping;
__block NSManagedObjectContext* moc;

beforeEach(^{
[MagicalRecord setDefaultModelFromClass:[self class]];
[MagicalRecord setupCoreDataStackWithInMemoryStore];

externalRepresentation = [CMFixture buildUsingFixture:@"Person"];
externalRepresentationPartial = [CMFixture buildUsingFixture:@"PersonWithoutRelations"];
moc = [NSManagedObjectContext MR_defaultContext];
[ManagedPerson registerMapping:[ManagedMappingProvider personMapping]];
[ManagedCar registerMapping:[ManagedMappingProvider carMapping]];
[ManagedPhone registerMapping:[ManagedMappingProvider phoneMapping]];
personMapping = [ManagedPerson objectMapping];
});

specify(^{
ManagedPerson *person = [EKManagedObjectMapper objectFromExternalRepresentation:externalRepresentation
withMapping:personMapping inManagedObjectContext:moc];

// Check default behaviour
[[person.car shouldNot] beNil];
[[person.phones shouldNot] beNil];

[EKManagedObjectMapper fillObject:person fromExternalRepresentation:externalRepresentationPartial withMapping:personMapping inManagedObjectContext:moc];

[[person.car should] beNil];
[[person.phones should] beEmpty];

// Check behaviour with set ignoreMissingFields property
person = [EKManagedObjectMapper objectFromExternalRepresentation:externalRepresentation
withMapping:personMapping inManagedObjectContext:moc];

personMapping.ignoreMissingFields = YES;
[[person.car shouldNot] beNil];
[[person.phones shouldNot] beNil];
[EKManagedObjectMapper fillObject:person fromExternalRepresentation:externalRepresentationPartial withMapping:personMapping inManagedObjectContext:moc];
[[person.car shouldNot] beNil];
[[person.phones shouldNot] beNil];
});

});
});

SPEC_END

0 comments on commit ac658bc

Please sign in to comment.