Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NSCopying support to FEMMapping #74

Merged
merged 4 commits into from
Sep 27, 2016
Merged
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
2 changes: 1 addition & 1 deletion FastEasyMapping/Source/Core/Mapping/FEMAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ NS_ASSUME_NONNULL_BEGIN

typedef _Nullable id (^FEMMapBlock)(id value);

@interface FEMAttribute : NSObject <FEMProperty>
@interface FEMAttribute : NSObject <NSCopying, FEMProperty>

- (nullable id)mapValue:(nullable id)value;
- (nullable id)reverseMapValue:(nullable id)value;
Expand Down
6 changes: 6 additions & 0 deletions FastEasyMapping/Source/Core/Mapping/FEMAttribute.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ + (instancetype)mappingOfProperty:(NSString *)property toKeyPath:(NSString *)key
return [[self alloc] initWithProperty:property keyPath:keyPath map:map reverseMap:reverseMap];
}

#pragma mark - NSCopying

- (instancetype)copyWithZone:(NSZone *)zone {
return [[self.class allocWithZone:zone] initWithProperty:self.property keyPath:self.keyPath map:_map reverseMap:_reverseMap];
}

#pragma mark - Description

- (NSString *)description {
Expand Down
6 changes: 3 additions & 3 deletions FastEasyMapping/Source/Core/Mapping/FEMMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

NS_ASSUME_NONNULL_BEGIN

@interface FEMMapping : NSObject
@interface FEMMapping : NSObject <NSCopying>

- (instancetype)init __attribute__((unavailable("use -[FEMMapping initWithObjectClass:] or -[FEMMapping initWithEntityName:] instead")));
+ (instancetype)new __attribute__((unavailable("use -[FEMMapping initWithObjectClass:] or -[FEMMapping initWithEntityName:] instead")));
Expand All @@ -19,8 +19,8 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithEntityName:(NSString *)entityName NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithEntityName:(NSString *)entityName rootPath:(nullable NSString *)rootPath;

@property (nonatomic, readonly, nullable) Class objectClass;
@property (nonatomic, copy, readonly, nullable) NSString *entityName;
@property (nonatomic, strong, nullable) Class objectClass;
@property (nonatomic, copy, nullable) NSString *entityName;

@property (nonatomic, copy, nullable) NSString *rootPath;

Expand Down
37 changes: 37 additions & 0 deletions FastEasyMapping/Source/Core/Mapping/FEMMapping.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ - (instancetype)initWithEntityName:(NSString *)entityName rootPath:(NSString *)r
return self;
}

#pragma mark - NSCopying

- (instancetype)copyWithZone:(NSZone *)zone {
FEMMapping *mapping = [self.class allocWithZone:zone];
if (self.objectClass) {
mapping = [mapping initWithObjectClass:self.objectClass];
} else {
mapping = [mapping initWithEntityName:self.entityName];
}
mapping.rootPath = self.rootPath;
mapping.primaryKey = self.primaryKey;

for (FEMAttribute *attribute in self.attributes) {
[mapping addAttribute:[attribute copy]];
}

for (FEMRelationship *relationship in self.relationships) {
[mapping addRelationship:[relationship copy]];
}

return mapping;
}

#pragma mark - Attribute Mapping

- (void)addPropertyMapping:(id<FEMProperty>)propertyMapping toMap:(NSMutableDictionary *)map {
Expand Down Expand Up @@ -101,6 +124,20 @@ - (NSArray *)relationships {
return [_relationshipMap allValues];
}

- (void)setEntityName:(NSString *)entityName {
_entityName = [entityName copy];
if (_entityName) {
_objectClass = nil;
}
}

- (void)setObjectClass:(Class)objectClass {
_objectClass = objectClass;
if (_objectClass) {
_entityName = nil;
}
}

#pragma mark -

- (FEMAttribute *)primaryKeyAttribute {
Expand Down
2 changes: 1 addition & 1 deletion FastEasyMapping/Source/Core/Mapping/FEMRelationship.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

NS_ASSUME_NONNULL_BEGIN

@interface FEMRelationship : NSObject <FEMProperty>
@interface FEMRelationship : NSObject <NSCopying, FEMProperty>

@property (nonatomic, strong) FEMMapping *mapping;
@property (nonatomic, getter=isToMany) BOOL toMany;
Expand Down
10 changes: 10 additions & 0 deletions FastEasyMapping/Source/Core/Mapping/FEMRelationship.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ - (instancetype)initWithProperty:(NSString *)property keyPath:(NSString *)keyPat
return self;
}

#pragma mark - NSCopying

- (instancetype)copyWithZone:(NSZone *)zone {
FEMRelationship *relationship = [[FEMRelationship allocWithZone:zone] initWithProperty:self.property keyPath:self.keyPath mapping:self.mapping];
relationship.assignmentPolicy = self.assignmentPolicy;
relationship.toMany = self.toMany;
relationship.weak = self.weak;
return relationship;
}

#pragma mark - Shortcut

- (void)setMapping:(nonnull FEMMapping *)mapping forKeyPath:(nullable NSString *)keyPath {
Expand Down