Skip to content
Browse files

Refactor code to not use higher-order functions

  • Loading branch information...
1 parent 70951d1 commit 390cccde4e21032e2237833cac1dac6c8c8ad4c6 @jspahrsummers jspahrsummers committed
View
9 Mantle/NSArray+MTLManipulationAdditions.m
@@ -7,18 +7,17 @@
//
#import "NSArray+MTLManipulationAdditions.h"
-#import "NSArray+MTLHigherOrderAdditions.h"
@implementation NSArray (MTLManipulationAdditions)
- (id)mtl_firstObject {
- return self.count > 0 ? [self objectAtIndex:0] : nil;
+ return self.count > 0 ? self[0] : nil;
}
- (instancetype)mtl_arrayByRemovingObject:(id)object {
- return [self mtl_filterUsingBlock:^ BOOL (id arrayObject) {
- return ![arrayObject isEqual:object];
- }];
+ NSMutableArray *result = [self mutableCopy];
+ [result removeObject:object];
+ return result;
}
- (instancetype)mtl_arrayByRemovingFirstObject {
View
10 Mantle/NSDictionary+MTLManipulationAdditions.m
@@ -7,21 +7,19 @@
//
#import "NSDictionary+MTLManipulationAdditions.h"
-#import "NSDictionary+MTLHigherOrderAdditions.h"
@implementation NSDictionary (MTLManipulationAdditions)
- (NSDictionary *)mtl_dictionaryByAddingEntriesFromDictionary:(NSDictionary *)dictionary {
NSMutableDictionary *result = [self mutableCopy];
[result addEntriesFromDictionary:dictionary];
-
- return [result copy];
+ return result;
}
- (NSDictionary *)mtl_dictionaryByRemovingEntriesWithKeys:(NSSet *)keys {
- return [self mtl_filterEntriesUsingBlock:^ BOOL (id key, id value) {
- return ![keys containsObject:key];
- }];
+ NSMutableDictionary *result = [self mutableCopy];
+ [result removeObjectsForKeys:keys.allObjects];
+ return result;
}
@end
View
25 Mantle/NSValueTransformer+MTLPredefinedTransformerAdditions.m
@@ -10,7 +10,6 @@
#import "MTLJSONAdapter.h"
#import "MTLModel.h"
#import "MTLValueTransformer.h"
-#import "NSArray+MTLHigherOrderAdditions.h"
NSString * const MTLURLValueTransformerName = @"MTLURLValueTransformerName";
NSString * const MTLBooleanValueTransformerName = @"MTLBooleanValueTransformerName";
@@ -76,18 +75,30 @@ + (NSValueTransformer *)mtl_JSONArrayTransformerWithModelClass:(Class)modelClass
NSAssert([dictionaries isKindOfClass:NSArray.class], @"Expected a array of dictionaries, got: %@", dictionaries);
- return [dictionaries mtl_mapUsingBlock:^(NSDictionary *JSONDictionary) {
- return [dictionaryTransformer transformedValue:JSONDictionary];
- }];
+ NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictionaries.count];
+ for (NSDictionary *JSONDictionary in dictionaries) {
+ id model = [dictionaryTransformer transformedValue:JSONDictionary];
+ if (model == nil) continue;
+
+ [models addObject:model];
+ }
+
+ return models;
}
reverseBlock:^ id (NSArray *models) {
if (models == nil) return nil;
NSAssert([models isKindOfClass:NSArray.class], @"Expected a array of MTLModels, got: %@", models);
- return [models mtl_mapUsingBlock:^(MTLModel *model) {
- return [dictionaryTransformer reverseTransformedValue:model];
- }];
+ NSMutableArray *dictionaries = [NSMutableArray arrayWithCapacity:models.count];
+ for (MTLModel *model in models) {
+ NSDictionary *dict = [dictionaryTransformer reverseTransformedValue:model];
+ if (dict == nil) continue;
+
+ [dictionaries addObject:dict];
+ }
+
+ return dictionaries;
}];
}
View
13 MantleTests/MTLPredefinedTransformerAdditionsSpec.m
@@ -55,21 +55,22 @@
beforeEach(^{
NSMutableArray *uniqueModels = [NSMutableArray array];
+ NSMutableArray *mutableDictionaries = [NSMutableArray array];
+
for (NSUInteger i = 0; i < 10; i++) {
MTLTestModel *model = [[MTLTestModel alloc] init];
model.count = i;
[uniqueModels addObject:model];
- }
-
- models = [uniqueModels copy];
- JSONDictionaries = [uniqueModels mtl_mapUsingBlock:^(MTLTestModel *model) {
NSDictionary *dict = [MTLJSONAdapter JSONDictionaryFromModel:model];
expect(dict).notTo.beNil();
- return dict;
- }];
+ [mutableDictionaries addObject:dict];
+ }
+
+ models = [uniqueModels copy];
+ JSONDictionaries = [mutableDictionaries copy];
});
describe(@"dictionary transformer", ^{

0 comments on commit 390cccd

Please sign in to comment.
Something went wrong with that request. Please try again.