Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Classes/NSManagedObject+Mappings.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#import "NSManagedObject+ActiveRecord.h"
#import "ObjectiveSugar.h"

typedef id (^TransformBlock)(id value, NSManagedObjectContext *context);

@implementation NSManagedObject (Mappings)

+ (NSString *)keyForRemoteKey:(NSString *)remoteKey inContext:(NSManagedObjectContext *)context {
Expand All @@ -46,6 +48,11 @@ + (NSString *)keyForRemoteKey:(NSString *)remoteKey inContext:(NSManagedObjectCo
}

+ (id)transformValue:(id)value forRemoteKey:(NSString *)remoteKey inContext:(NSManagedObjectContext *)context {
if ([self cachedMappings][remoteKey][@"transform"]) {
TransformBlock transformer = [self cachedMappings][remoteKey][@"transform"];
return transformer(value, context);
}

Class class = [self cachedMappings][remoteKey][@"class"];
if (class)
return [self objectOrSetOfObjectsFromValue:value ofClass:class inContext:context];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "92C36FE96BF5420EBF7F892F"
BlueprintIdentifier = "B856C4C9923B4EC7A757D776"
BuildableName = "libPods.a"
BlueprintName = "Pods"
ReferencedContainer = "container:Pods/Pods.xcodeproj">
Expand All @@ -28,7 +28,7 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "87598C52275746D1A1FE32AE"
BlueprintIdentifier = "8B11F5055ECD4BB49B3C08A4"
BuildableName = "libPods-SampleProjectTests.a"
BlueprintName = "Pods-SampleProjectTests"
ReferencedContainer = "container:Pods/Pods.xcodeproj">
Expand Down
11 changes: 7 additions & 4 deletions Example/SampleProject/Models/Categories/Car+Mappings.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ + (NSDictionary *)mappings {
@"class": [InsuranceCompany class]
},
@"insurance_company": @{
@"key": @"insuranceCompany",
@"class": [InsuranceCompany class]
}

@"key": @"insuranceCompany",
@"transform": ^id(NSDictionary *value, NSManagedObjectContext *context) {
InsuranceCompany * company = [InsuranceCompany findOrCreate:@{@"remoteID": value[@"id"] ?: value[@"remoteID"]} inContext:context];
[company update:value];
return company;
}
}
};
}

Expand Down
5 changes: 5 additions & 0 deletions Example/SampleProjectTests/MappingsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
Person *bob = [Person findOrCreate:@{ @"first_name": @"Bob" }];
[[bob.firstName should] equal:@"Bob"];
});

it(@"uses mappings to transform values", ^{
Car *car = [Car create:@{ @"hp": @150, @"insurance_company": @{ @"name" : @"Farmers", @"remoteID" : @4567 } }];
[[car.insuranceCompany.name should] equal:@"Farmers"];
});

it(@"supports creating a parent object using just ID from the server", ^{
Car *car = [Car create:@{ @"hp": @150, @"insurance_id": @1234 }];
Expand Down