Skip to content

Commit

Permalink
implement method for converting underscore properties to camel case. …
Browse files Browse the repository at this point in the history
…Bump podspec.
  • Loading branch information
DenTelezhkin committed Jul 1, 2016
1 parent adbc411 commit 07d6d74
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 4 deletions.
2 changes: 1 addition & 1 deletion EasyMapping.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "EasyMapping"
s.version = "0.17.1"
s.version = "0.18.0"
s.summary = "The easiest way to map data from your webservice."
s.homepage = "https://github.com/lucasmedeirosleite/EasyMapping"
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
11 changes: 9 additions & 2 deletions EasyMapping/EKObjectMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
Root JSON path. This is helpful, when all object data is inside another JSON dictionary.
*/
@property (nonatomic, strong, readonly) NSString *rootPath;
@property (nonatomic, strong, readonly, nullable) NSString *rootPath;

/**
Dictionary, containing property mappings for current object.
Expand Down Expand Up @@ -131,7 +131,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)mapKeyPath:(NSString *)keyPath toProperty:(NSString *)property;

/**
Map JSON keyPath to object property. This method assumes, that value contains NSString, that can be transformed into NSDate by NSDateFormatter. You can get current thread date formatter by using NSDateFormatter+EasyMappingAdditions category. Default timezone is GMT. Default date format ISO 8601.
Map JSON keyPath to object property. This method assumes, that value contains NSString, that can be transformed into NSDate by NSDateFormatter.
@param keyPath JSON keypath, that will be used by valueForKeyPath: method
Expand All @@ -155,6 +155,13 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)mapPropertiesFromArrayToPascalCase:(NSArray *)propertyNamesArray;

/**
Maps properties from array, making all keypaths that contain underscores - camel cased. For example, @"created_at" field in JSON will be mapped to @"createdAt" property on your model.
@param propertyNamesArray Array of property names.
*/
- (void)mapPropertiesFromUnderscoreToCamelCase:(NSArray *)propertyNamesArray;

/**
Maps properties from dictionary. Keys are keypaths in JSON, values are names of properties.
Expand Down
10 changes: 10 additions & 0 deletions EasyMapping/EKObjectMapping.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#import "EKRelationshipMapping.h"
#import "EKMappingProtocol.h"
#import "NSDateFormatter+EasyMappingAdditions.h"
#import "EKpropertyHelper.h"

@implementation EKObjectMapping

Expand Down Expand Up @@ -119,6 +120,15 @@ -(void)mapPropertiesFromArrayToPascalCase:(NSArray *)propertyNamesArray
}
}

- (void)mapPropertiesFromUnderscoreToCamelCase:(NSArray *)propertyNamesArray
{
for (NSString *key in propertyNamesArray) {
NSString *convertedKey = [EKPropertyHelper convertStringFromUnderScoreToCamelCase: key];

[self mapKeyPath:key toProperty:convertedKey];
}
}

- (void)mapPropertiesFromDictionary:(NSDictionary *)propertyDictionary
{
NSParameterAssert([propertyDictionary isKindOfClass:[NSDictionary class]]);
Expand Down
2 changes: 2 additions & 0 deletions EasyMapping/EKPropertyHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ NS_ASSUME_NONNULL_BEGIN
+ (nullable NSDictionary *)extractRootPathFromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKObjectMapping *)mapping;

+ (NSString *)convertStringFromUnderScoreToCamelCase:(NSString *)string;

@end

NS_ASSUME_NONNULL_END
17 changes: 17 additions & 0 deletions EasyMapping/EKPropertyHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,21 @@ + (NSDictionary *)extractRootPathFromExternalRepresentation:(NSDictionary *)exte
}
return externalRepresentation;
}

+ (NSString *)convertStringFromUnderScoreToCamelCase:(NSString *)string {
NSMutableString *output = [NSMutableString string];
BOOL makeNextCharacterUpperCase = NO;
for (NSInteger idx = 0; idx < [string length]; idx += 1) {
unichar c = [string characterAtIndex:idx];
if (c == '_') {
makeNextCharacterUpperCase = YES;
} else if (makeNextCharacterUpperCase) {
[output appendString:[[NSString stringWithCharacters:&c length:1] uppercaseString]];
makeNextCharacterUpperCase = NO;
} else {
[output appendFormat:@"%C", c];
}
}
return output;
}
@end
2 changes: 1 addition & 1 deletion EasyMapping/NSDateFormatter+EasyMappingAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static NSString * const EKISO_8601DateTimeFormat = @"yyyy-MM-dd";
/**
NSDateFormatter instance for current NSThread. It is lazily constructed, default date format - ISO 8601.
Thos property is deprecated and stated to be removed in release for Xcode 8, which drops support for iOS 7 and lower.
Thos property is deprecated and slated to be removed in release for Xcode 8, which drops support for iOS 7 and lower.
*/
+ (NSDateFormatter *)ek_formatterForCurrentThread DEPRECATED_ATTRIBUTE;

Expand Down
54 changes: 54 additions & 0 deletions EasyMappingExample/Tests/Specs/EKObjectMappingSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,60 @@

});

describe(@"#mapPropertiesFromUnderscoreToCamelCase", ^{

__block EKObjectMapping *mapping;

beforeEach(^{
mapping = [[EKObjectMapping alloc] initWithObjectClass:[Car class]];
[mapping mapPropertiesFromUnderscoreToCamelCase:@[@"model", @"year", @"created_at",@"car_id"]];
});

describe(@"createdAt field", ^{

__block EKPropertyMapping *propertyMapping;

beforeEach(^{
propertyMapping = [mapping.propertyMappings objectForKey:@"created_at"];
});

specify(^{
[[propertyMapping shouldNot] beNil];
});

specify(^{
[[propertyMapping.keyPath should] equal:@"created_at"];
});

specify(^{
[[propertyMapping.property should] equal:@"createdAt"];
});
});

describe(@"car_id field", ^{

__block EKPropertyMapping *propertyMapping;

beforeEach(^{
propertyMapping = [mapping.propertyMappings objectForKey:@"car_id"];
});

specify(^{
[[propertyMapping shouldNot] beNil];
});

specify(^{
[[propertyMapping.keyPath should] equal:@"car_id"];
});

specify(^{
[[propertyMapping.property should] equal:@"carId"];
});

});

});


describe(@"#mapPropertiesFromDictionary", ^{

Expand Down

0 comments on commit 07d6d74

Please sign in to comment.